Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/soc/intel/avs/boards/es8336.c
29270 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
//
3
// Copyright(c) 2023 Intel Corporation
4
//
5
// Authors: Cezary Rojewski <[email protected]>
6
// Amadeusz Slawinski <[email protected]>
7
//
8
9
#include <linux/device.h>
10
#include <linux/gpio/consumer.h>
11
#include <linux/input.h>
12
#include <linux/module.h>
13
#include <linux/platform_device.h>
14
#include <linux/processor.h>
15
#include <linux/slab.h>
16
#include <sound/jack.h>
17
#include <sound/pcm.h>
18
#include <sound/pcm_params.h>
19
#include <sound/soc.h>
20
#include <sound/soc-acpi.h>
21
#include <asm/cpu_device_id.h>
22
#include "../utils.h"
23
24
#define ES8336_CODEC_DAI "ES8316 HiFi"
25
26
struct avs_card_drvdata {
27
struct snd_soc_jack jack;
28
struct gpio_desc *gpiod;
29
};
30
31
static const struct acpi_gpio_params enable_gpio = { 0, 0, true };
32
33
static const struct acpi_gpio_mapping speaker_gpios[] = {
34
{ "speaker-enable-gpios", &enable_gpio, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
35
{ }
36
};
37
38
static int avs_es8336_speaker_power_event(struct snd_soc_dapm_widget *w,
39
struct snd_kcontrol *kcontrol, int event)
40
{
41
struct snd_soc_card *card = w->dapm->card;
42
struct avs_card_drvdata *data;
43
bool speaker_en;
44
45
data = snd_soc_card_get_drvdata(card);
46
/* As enable_gpio has active_low=true, logic is inverted. */
47
speaker_en = !SND_SOC_DAPM_EVENT_ON(event);
48
49
gpiod_set_value_cansleep(data->gpiod, speaker_en);
50
return 0;
51
}
52
53
static const struct snd_soc_dapm_widget card_widgets[] = {
54
SND_SOC_DAPM_SPK("Speaker", NULL),
55
SND_SOC_DAPM_HP("Headphone", NULL),
56
SND_SOC_DAPM_MIC("Headset Mic", NULL),
57
SND_SOC_DAPM_MIC("Internal Mic", NULL),
58
59
SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
60
avs_es8336_speaker_power_event,
61
SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
62
};
63
64
static const struct snd_soc_dapm_route card_routes[] = {
65
{"Headphone", NULL, "HPOL"},
66
{"Headphone", NULL, "HPOR"},
67
68
/*
69
* There is no separate speaker output instead the speakers are muxed to
70
* the HP outputs. The mux is controlled by the "Speaker Power" widget.
71
*/
72
{"Speaker", NULL, "HPOL"},
73
{"Speaker", NULL, "HPOR"},
74
{"Speaker", NULL, "Speaker Power"},
75
76
/* Mic route map */
77
{"MIC1", NULL, "Internal Mic"},
78
{"MIC2", NULL, "Headset Mic"},
79
};
80
81
static const struct snd_kcontrol_new card_controls[] = {
82
SOC_DAPM_PIN_SWITCH("Speaker"),
83
SOC_DAPM_PIN_SWITCH("Headphone"),
84
SOC_DAPM_PIN_SWITCH("Headset Mic"),
85
SOC_DAPM_PIN_SWITCH("Internal Mic"),
86
};
87
88
static const struct snd_soc_jack_pin card_headset_pins[] = {
89
{
90
.pin = "Headphone",
91
.mask = SND_JACK_HEADPHONE,
92
},
93
{
94
.pin = "Headset Mic",
95
.mask = SND_JACK_MICROPHONE,
96
},
97
};
98
99
static int avs_es8336_codec_init(struct snd_soc_pcm_runtime *runtime)
100
{
101
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
102
struct snd_soc_component *component = codec_dai->component;
103
struct snd_soc_card *card = runtime->card;
104
struct snd_soc_jack_pin *pins;
105
struct avs_card_drvdata *data;
106
struct gpio_desc *gpiod;
107
int num_pins, ret;
108
109
data = snd_soc_card_get_drvdata(card);
110
num_pins = ARRAY_SIZE(card_headset_pins);
111
112
pins = devm_kmemdup_array(card->dev, card_headset_pins, num_pins,
113
sizeof(card_headset_pins[0]), GFP_KERNEL);
114
if (!pins)
115
return -ENOMEM;
116
117
ret = snd_soc_card_jack_new_pins(card, "Headset Jack", SND_JACK_HEADSET | SND_JACK_BTN_0,
118
&data->jack, pins, num_pins);
119
if (ret)
120
return ret;
121
122
ret = devm_acpi_dev_add_driver_gpios(codec_dai->dev, speaker_gpios);
123
if (ret)
124
dev_warn(codec_dai->dev, "Unable to add GPIO mapping table\n");
125
126
gpiod = gpiod_get_optional(codec_dai->dev, "speaker-enable", GPIOD_OUT_LOW);
127
if (IS_ERR(gpiod))
128
return dev_err_probe(codec_dai->dev, PTR_ERR(gpiod), "Get gpiod failed: %ld\n",
129
PTR_ERR(gpiod));
130
131
data->gpiod = gpiod;
132
snd_jack_set_key(data->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
133
snd_soc_component_set_jack(component, &data->jack, NULL);
134
135
card->dapm.idle_bias = false;
136
137
return 0;
138
}
139
140
static void avs_es8336_codec_exit(struct snd_soc_pcm_runtime *runtime)
141
{
142
struct avs_card_drvdata *data = snd_soc_card_get_drvdata(runtime->card);
143
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
144
145
snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
146
gpiod_put(data->gpiod);
147
}
148
149
static int avs_es8336_hw_params(struct snd_pcm_substream *substream,
150
struct snd_pcm_hw_params *params)
151
{
152
struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream);
153
struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
154
int clk_freq;
155
int ret;
156
157
switch (boot_cpu_data.x86_vfm) {
158
case INTEL_KABYLAKE_L:
159
case INTEL_KABYLAKE:
160
clk_freq = 24000000;
161
break;
162
default:
163
clk_freq = 19200000;
164
break;
165
}
166
167
ret = snd_soc_dai_set_sysclk(codec_dai, 1, clk_freq, SND_SOC_CLOCK_OUT);
168
if (ret < 0)
169
dev_err(runtime->dev, "Set codec sysclk failed: %d\n", ret);
170
171
return ret;
172
}
173
174
static const struct snd_soc_ops avs_es8336_ops = {
175
.hw_params = avs_es8336_hw_params,
176
};
177
178
static int avs_es8336_be_fixup(struct snd_soc_pcm_runtime *runtime,
179
struct snd_pcm_hw_params *params)
180
{
181
struct snd_interval *rate, *channels;
182
struct snd_mask *fmt;
183
184
rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
185
channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
186
fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
187
188
/* The ADSP will convert the FE rate to 48k, stereo */
189
rate->min = rate->max = 48000;
190
channels->min = channels->max = 2;
191
192
/* set SSPN to 24 bit */
193
snd_mask_none(fmt);
194
snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_3LE);
195
196
return 0;
197
}
198
199
static int avs_create_dai_link(struct device *dev, int ssp_port, int tdm_slot,
200
struct snd_soc_dai_link **dai_link)
201
{
202
struct snd_soc_dai_link_component *platform;
203
struct snd_soc_dai_link *dl;
204
205
dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
206
platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
207
if (!dl || !platform)
208
return -ENOMEM;
209
210
dl->name = devm_kasprintf(dev, GFP_KERNEL,
211
AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
212
dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
213
dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
214
if (!dl->name || !dl->cpus || !dl->codecs)
215
return -ENOMEM;
216
217
dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
218
AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
219
dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-ESSX8336:00");
220
dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, ES8336_CODEC_DAI);
221
if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
222
return -ENOMEM;
223
224
platform->name = dev_name(dev);
225
dl->num_cpus = 1;
226
dl->num_codecs = 1;
227
dl->platforms = platform;
228
dl->num_platforms = 1;
229
dl->id = 0;
230
dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
231
dl->init = avs_es8336_codec_init;
232
dl->exit = avs_es8336_codec_exit;
233
dl->be_hw_params_fixup = avs_es8336_be_fixup;
234
dl->ops = &avs_es8336_ops;
235
dl->nonatomic = 1;
236
dl->no_pcm = 1;
237
238
*dai_link = dl;
239
240
return 0;
241
}
242
243
static int avs_card_suspend_pre(struct snd_soc_card *card)
244
{
245
struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, ES8336_CODEC_DAI);
246
247
return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
248
}
249
250
static int avs_card_resume_post(struct snd_soc_card *card)
251
{
252
struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, ES8336_CODEC_DAI);
253
struct avs_card_drvdata *data = snd_soc_card_get_drvdata(card);
254
255
return snd_soc_component_set_jack(codec_dai->component, &data->jack, NULL);
256
}
257
258
static int avs_es8336_probe(struct platform_device *pdev)
259
{
260
struct snd_soc_dai_link *dai_link;
261
struct snd_soc_acpi_mach *mach;
262
struct avs_mach_pdata *pdata;
263
struct avs_card_drvdata *data;
264
struct snd_soc_card *card;
265
struct device *dev = &pdev->dev;
266
int ssp_port, tdm_slot, ret;
267
268
mach = dev_get_platdata(dev);
269
pdata = mach->pdata;
270
271
ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
272
if (ret)
273
return ret;
274
275
ret = avs_create_dai_link(dev, ssp_port, tdm_slot, &dai_link);
276
if (ret) {
277
dev_err(dev, "Failed to create dai link: %d", ret);
278
return ret;
279
}
280
281
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
282
card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
283
if (!data || !card)
284
return -ENOMEM;
285
286
if (pdata->obsolete_card_names) {
287
card->name = "avs_es8336";
288
} else {
289
card->driver_name = "avs_es8336";
290
card->long_name = card->name = "AVS I2S ES8336";
291
}
292
card->dev = dev;
293
card->owner = THIS_MODULE;
294
card->suspend_pre = avs_card_suspend_pre;
295
card->resume_post = avs_card_resume_post;
296
card->dai_link = dai_link;
297
card->num_links = 1;
298
card->controls = card_controls;
299
card->num_controls = ARRAY_SIZE(card_controls);
300
card->dapm_widgets = card_widgets;
301
card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
302
card->dapm_routes = card_routes;
303
card->num_dapm_routes = ARRAY_SIZE(card_routes);
304
card->fully_routed = true;
305
snd_soc_card_set_drvdata(card, data);
306
307
return devm_snd_soc_register_deferrable_card(dev, card);
308
}
309
310
static const struct platform_device_id avs_es8336_driver_ids[] = {
311
{
312
.name = "avs_es8336",
313
},
314
{},
315
};
316
MODULE_DEVICE_TABLE(platform, avs_es8336_driver_ids);
317
318
static struct platform_driver avs_es8336_driver = {
319
.probe = avs_es8336_probe,
320
.driver = {
321
.name = "avs_es8336",
322
.pm = &snd_soc_pm_ops,
323
},
324
.id_table = avs_es8336_driver_ids,
325
};
326
327
module_platform_driver(avs_es8336_driver);
328
329
MODULE_DESCRIPTION("Intel es8336 machine driver");
330
MODULE_LICENSE("GPL");
331
332