Debian Codegraph @

sound/soc/amd/acp/acp3x-es83xx/acp3x-es83xx.c

raw
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Machine driver for AMD ACP Audio engine using ES8336 codec.
4 //
5 // Copyright 2023 Marian Postevca <[email protected]>
6 #include <sound/core.h>
7 #include <sound/soc.h>
8 #include <sound/pcm.h>
9 #include <sound/pcm_params.h>
10 #include <sound/soc-dapm.h>
11 #include <sound/jack.h>
12 #include <sound/soc-acpi.h>
13 #include <linux/clk.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/input.h>
18 #include <linux/io.h>
19 #include <linux/acpi.h>
20 #include <linux/dmi.h>
21 #include <linux/string_choices.h>
22 #include "../acp-mach.h"
23 #include "acp3x-es83xx.h"
24
25 #define get_mach_priv(card) ((struct acp3x_es83xx_private *)((acp_get_drvdata(card))->mach_priv))
26
27 #define DUAL_CHANNEL 2
28
29 #define ES83XX_ENABLE_DMIC BIT(4)
30 #define ES83XX_48_MHZ_MCLK BIT(5)
31
32 struct acp3x_es83xx_private {
33 bool speaker_on;
34 bool headphone_on;
35 unsigned long quirk;
36 struct snd_soc_component *codec;
37 struct device *codec_dev;
38 struct gpio_desc *gpio_speakers, *gpio_headphone;
39 struct acpi_gpio_params enable_spk_gpio, enable_hp_gpio;
40 struct acpi_gpio_mapping gpio_mapping[3];
41 struct snd_soc_dapm_route mic_map[2];
42 };
43
44 static const unsigned int channels[] = {
45 DUAL_CHANNEL,
46 };
47
48 static const struct snd_pcm_hw_constraint_list constraints_channels = {
49 .count = ARRAY_SIZE(channels),
50 .list = channels,
51 .mask = 0,
52 };
53
54 #define ES83xx_12288_KHZ_MCLK_FREQ (48000 * 256)
55 #define ES83xx_48_MHZ_MCLK_FREQ (48000 * 1000)
56
57 static int acp3x_es83xx_headphone_power_event(struct snd_soc_dapm_widget *w,
58 struct snd_kcontrol *kcontrol, int event);
59 static int acp3x_es83xx_speaker_power_event(struct snd_soc_dapm_widget *w,
60 struct snd_kcontrol *kcontrol, int event);
61
62 static int acp3x_es83xx_codec_startup(struct snd_pcm_substream *substream)
63 {
64 struct snd_pcm_runtime *runtime;
65 struct snd_soc_pcm_runtime *rtd;
66 struct snd_soc_dai *codec_dai;
67 struct acp3x_es83xx_private *priv;
68 unsigned int freq;
69 int ret;
70
71 runtime = substream->runtime;
72 rtd = snd_soc_substream_to_rtd(substream);
73 codec_dai = snd_soc_rtd_to_codec(rtd, 0);
74 priv = get_mach_priv(rtd->card);
75
76 if (priv->quirk & ES83XX_48_MHZ_MCLK) {
77 dev_dbg(priv->codec_dev, "using a 48Mhz MCLK\n");
78 freq = ES83xx_48_MHZ_MCLK_FREQ;
79 } else {
80 dev_dbg(priv->codec_dev, "using a 12.288Mhz MCLK\n");
81 freq = ES83xx_12288_KHZ_MCLK_FREQ;
82 }
83
84 ret = snd_soc_dai_set_sysclk(codec_dai, 0, freq, SND_SOC_CLOCK_OUT);
85 if (ret < 0) {
86 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
87 return ret;
88 }
89
90 runtime->hw.channels_max = DUAL_CHANNEL;
91 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
92 &constraints_channels);
93
94 return 0;
95 }
96
97 static struct snd_soc_jack es83xx_jack;
98
99 static struct snd_soc_jack_pin es83xx_jack_pins[] = {
100 {
101 .pin = "Headphone",
102 .mask = SND_JACK_HEADPHONE,
103 },
104 {
105 .pin = "Headset Mic",
106 .mask = SND_JACK_MICROPHONE,
107 },
108 };
109
110 static const struct snd_soc_dapm_widget acp3x_es83xx_widgets[] = {
111 SND_SOC_DAPM_SPK("Speaker", NULL),
112 SND_SOC_DAPM_HP("Headphone", NULL),
113 SND_SOC_DAPM_MIC("Headset Mic", NULL),
114 SND_SOC_DAPM_MIC("Internal Mic", NULL),
115
116 SND_SOC_DAPM_SUPPLY("Headphone Power", SND_SOC_NOPM, 0, 0,
117 acp3x_es83xx_headphone_power_event,
118 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
119 SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0,
120 acp3x_es83xx_speaker_power_event,
121 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
122 };
123
124 static const struct snd_soc_dapm_route acp3x_es83xx_audio_map[] = {
125 {"Headphone", NULL, "HPOL"},
126 {"Headphone", NULL, "HPOR"},
127 {"Headphone", NULL, "Headphone Power"},
128
129 /*
130 * There is no separate speaker output instead the speakers are muxed to
131 * the HP outputs. The mux is controlled Speaker and/or headphone switch.
132 */
133 {"Speaker", NULL, "HPOL"},
134 {"Speaker", NULL, "HPOR"},
135 {"Speaker", NULL, "Speaker Power"},
136 };
137
138
139 static const struct snd_kcontrol_new acp3x_es83xx_controls[] = {
140 SOC_DAPM_PIN_SWITCH("Speaker"),
141 SOC_DAPM_PIN_SWITCH("Headphone"),
142 SOC_DAPM_PIN_SWITCH("Headset Mic"),
143 SOC_DAPM_PIN_SWITCH("Internal Mic"),
144 };
145
146 static int acp3x_es83xx_configure_widgets(struct snd_soc_card *card)
147 {
148 card->dapm_widgets = acp3x_es83xx_widgets;
149 card->num_dapm_widgets = ARRAY_SIZE(acp3x_es83xx_widgets);
150 card->controls = acp3x_es83xx_controls;
151 card->num_controls = ARRAY_SIZE(acp3x_es83xx_controls);
152 card->dapm_routes = acp3x_es83xx_audio_map;
153 card->num_dapm_routes = ARRAY_SIZE(acp3x_es83xx_audio_map);
154
155 return 0;
156 }
157
158 static int acp3x_es83xx_headphone_power_event(struct snd_soc_dapm_widget *w,
159 struct snd_kcontrol *kcontrol, int event)
160 {
161 struct snd_soc_card *card = snd_soc_dapm_to_card(w->dapm);
162 struct acp3x_es83xx_private *priv = get_mach_priv(card);
163
164 dev_dbg(priv->codec_dev, "headphone power event = %d\n", event);
165 if (SND_SOC_DAPM_EVENT_ON(event))
166 priv->headphone_on = true;
167 else
168 priv->headphone_on = false;
169
170 gpiod_set_value_cansleep(priv->gpio_speakers, priv->speaker_on);
171 gpiod_set_value_cansleep(priv->gpio_headphone, priv->headphone_on);
172
173 return 0;
174 }
175
176 static int acp3x_es83xx_speaker_power_event(struct snd_soc_dapm_widget *w,
177 struct snd_kcontrol *kcontrol, int event)
178 {
179 struct snd_soc_card *card = snd_soc_dapm_to_card(w->dapm);
180 struct acp3x_es83xx_private *priv = get_mach_priv(card);
181
182 dev_dbg(priv->codec_dev, "speaker power event: %d\n", event);
183 if (SND_SOC_DAPM_EVENT_ON(event))
184 priv->speaker_on = true;
185 else
186 priv->speaker_on = false;
187
188 gpiod_set_value_cansleep(priv->gpio_speakers, priv->speaker_on);
189 gpiod_set_value_cansleep(priv->gpio_headphone, priv->headphone_on);
190
191 return 0;
192 }
193
194 static int acp3x_es83xx_suspend_pre(struct snd_soc_card *card)
195 {
196 struct acp3x_es83xx_private *priv = get_mach_priv(card);
197
198 /* We need to disable the jack in the machine driver suspend
199 * callback so that the CODEC suspend callback actually gets
200 * called. Without doing it, the CODEC suspend/resume
201 * callbacks do not get called if headphones are plugged in.
202 * This is because plugging in headphones keeps some supplies
203 * active, this in turn means that the lowest bias level
204 * that the CODEC can go to is SND_SOC_BIAS_STANDBY.
205 * If components do not set idle_bias_on to true then
206 * their suspend/resume callbacks do not get called.
207 */
208 dev_dbg(priv->codec_dev, "card suspend\n");
209 snd_soc_component_set_jack(priv->codec, NULL, NULL);
210 return 0;
211 }
212
213 static int acp3x_es83xx_resume_post(struct snd_soc_card *card)
214 {
215 struct acp3x_es83xx_private *priv = get_mach_priv(card);
216
217 /* We disabled jack detection in suspend callback,
218 * enable it back.
219 */
220 dev_dbg(priv->codec_dev, "card resume\n");
221 snd_soc_component_set_jack(priv->codec, &es83xx_jack, NULL);
222 return 0;
223 }
224
225 static int acp3x_es83xx_configure_gpios(struct acp3x_es83xx_private *priv)
226 {
227
228 priv->enable_spk_gpio.crs_entry_index = 0;
229 priv->enable_hp_gpio.crs_entry_index = 1;
230
231 priv->enable_spk_gpio.active_low = false;
232 priv->enable_hp_gpio.active_low = false;
233
234 priv->gpio_mapping[0].name = "speakers-enable-gpios";
235 priv->gpio_mapping[0].data = &priv->enable_spk_gpio;
236 priv->gpio_mapping[0].size = 1;
237 priv->gpio_mapping[0].quirks = ACPI_GPIO_QUIRK_ONLY_GPIOIO;
238
239 priv->gpio_mapping[1].name = "headphone-enable-gpios";
240 priv->gpio_mapping[1].data = &priv->enable_hp_gpio;
241 priv->gpio_mapping[1].size = 1;
242 priv->gpio_mapping[1].quirks = ACPI_GPIO_QUIRK_ONLY_GPIOIO;
243
244 dev_info(priv->codec_dev, "speaker gpio %d active %s, headphone gpio %d active %s\n",
245 priv->enable_spk_gpio.crs_entry_index,
246 str_low_high(priv->enable_spk_gpio.active_low),
247 priv->enable_hp_gpio.crs_entry_index,
248 str_low_high(priv->enable_hp_gpio.active_low));
249 return 0;
250 }
251
252 static int acp3x_es83xx_configure_mics(struct acp3x_es83xx_private *priv)
253 {
254 int num_routes = 0;
255 int i;
256
257 if (!(priv->quirk & ES83XX_ENABLE_DMIC)) {
258 priv->mic_map[num_routes].sink = "MIC1";
259 priv->mic_map[num_routes].source = "Internal Mic";
260 num_routes++;
261 }
262
263 priv->mic_map[num_routes].sink = "MIC2";
264 priv->mic_map[num_routes].source = "Headset Mic";
265 num_routes++;
266
267 for (i = 0; i < num_routes; i++)
268 dev_info(priv->codec_dev, "%s is %s\n",
269 priv->mic_map[i].source, priv->mic_map[i].sink);
270
271 return num_routes;
272 }
273
274 static int acp3x_es83xx_init(struct snd_soc_pcm_runtime *runtime)
275 {
276 struct snd_soc_component *codec = snd_soc_rtd_to_codec(runtime, 0)->component;
277 struct snd_soc_card *card = runtime->card;
278 struct acp3x_es83xx_private *priv = get_mach_priv(card);
279 int ret = 0;
280 int num_routes;
281
282 ret = snd_soc_card_jack_new_pins(card, "Headset",
283 SND_JACK_HEADSET | SND_JACK_BTN_0,
284 &es83xx_jack, es83xx_jack_pins,
285 ARRAY_SIZE(es83xx_jack_pins));
286 if (ret) {
287 dev_err(card->dev, "jack creation failed %d\n", ret);
288 return ret;
289 }
290
291 snd_jack_set_key(es83xx_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
292
293 snd_soc_component_set_jack(codec, &es83xx_jack, NULL);
294
295 priv->codec = codec;
296 acp3x_es83xx_configure_gpios(priv);
297
298 ret = devm_acpi_dev_add_driver_gpios(priv->codec_dev, priv->gpio_mapping);
299 if (ret)
300 dev_warn(priv->codec_dev, "failed to add speaker gpio\n");
301
302 priv->gpio_speakers = gpiod_get_optional(priv->codec_dev, "speakers-enable",
303 priv->enable_spk_gpio.active_low ? GPIOD_OUT_LOW : GPIOD_OUT_HIGH);
304 if (IS_ERR(priv->gpio_speakers)) {
305 dev_err(priv->codec_dev, "could not get speakers-enable GPIO\n");
306 return PTR_ERR(priv->gpio_speakers);
307 }
308
309 priv->gpio_headphone = gpiod_get_optional(priv->codec_dev, "headphone-enable",
310 priv->enable_hp_gpio.active_low ? GPIOD_OUT_LOW : GPIOD_OUT_HIGH);
311 if (IS_ERR(priv->gpio_headphone)) {
312 dev_err(priv->codec_dev, "could not get headphone-enable GPIO\n");
313 return PTR_ERR(priv->gpio_headphone);
314 }
315
316 num_routes = acp3x_es83xx_configure_mics(priv);
317 if (num_routes > 0) {
318 struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
319
320 ret = snd_soc_dapm_add_routes(dapm, priv->mic_map, num_routes);
321 if (ret != 0)
322 device_remove_software_node(priv->codec_dev);
323 }
324
325 return ret;
326 }
327
328 static const struct snd_soc_ops acp3x_es83xx_ops = {
329 .startup = acp3x_es83xx_codec_startup,
330 };
331
332
333 SND_SOC_DAILINK_DEF(codec,
334 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-ESSX8336:00", "ES8316 HiFi")));
335
336 static const struct dmi_system_id acp3x_es83xx_dmi_table[] = {
337 {
338 .matches = {
339 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "HUAWEI"),
340 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "KLVL-WXXW"),
341 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "M1010"),
342 },
343 .driver_data = (void *)(ES83XX_ENABLE_DMIC),
344 },
345 {
346 .matches = {
347 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "HUAWEI"),
348 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "KLVL-WXX9"),
349 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "M1010"),
350 },
351 .driver_data = (void *)(ES83XX_ENABLE_DMIC),
352 },
353 {
354 .matches = {
355 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "HUAWEI"),
356 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "BOM-WXX9"),
357 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "M1010"),
358 },
359 .driver_data = (void *)(ES83XX_ENABLE_DMIC|ES83XX_48_MHZ_MCLK),
360 },
361 {
362 .matches = {
363 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "HUAWEI"),
364 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HVY-WXX9"),
365 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "M1010"),
366 },
367 .driver_data = (void *)(ES83XX_ENABLE_DMIC),
368 },
369 {
370 .matches = {
371 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "HUAWEI"),
372 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HVY-WXX9"),
373 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "M1020"),
374 },
375 .driver_data = (void *)(ES83XX_ENABLE_DMIC),
376 },
377 {
378 .matches = {
379 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "HUAWEI"),
380 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HVY-WXX9"),
381 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "M1040"),
382 },
383 .driver_data = (void *)(ES83XX_ENABLE_DMIC),
384 },
385 {}
386 };
387
388 static int acp3x_es83xx_configure_link(struct snd_soc_card *card, struct snd_soc_dai_link *link)
389 {
390 link->codecs = codec;
391 link->num_codecs = ARRAY_SIZE(codec);
392 link->init = acp3x_es83xx_init;
393 link->ops = &acp3x_es83xx_ops;
394 link->dai_fmt = SND_SOC_DAIFMT_I2S
395 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBP_CFP;
396
397 return 0;
398 }
399
400 static int acp3x_es83xx_probe(struct snd_soc_card *card)
401 {
402 int ret = 0;
403 struct device *dev = card->dev;
404 const struct dmi_system_id *dmi_id;
405
406 dmi_id = dmi_first_match(acp3x_es83xx_dmi_table);
407 if (dmi_id && dmi_id->driver_data) {
408 struct acp3x_es83xx_private *priv;
409 struct acp_card_drvdata *acp_drvdata;
410 struct acpi_device *adev;
411 struct device *codec_dev;
412
413 acp_drvdata = (struct acp_card_drvdata *)card->drvdata;
414
415 dev_info(dev, "matched DMI table with this system, trying to register sound card\n");
416
417 adev = acpi_dev_get_first_match_dev(acp_drvdata->acpi_mach->id, NULL, -1);
418 if (!adev) {
419 dev_err(dev, "Error cannot find '%s' dev\n", acp_drvdata->acpi_mach->id);
420 return -ENXIO;
421 }
422
423 codec_dev = acpi_get_first_physical_node(adev);
424 acpi_dev_put(adev);
425 if (!codec_dev) {
426 dev_warn(dev, "Error cannot find codec device, will defer probe\n");
427 return -EPROBE_DEFER;
428 }
429
430 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
431 if (!priv) {
432 put_device(codec_dev);
433 return -ENOMEM;
434 }
435
436 priv->codec_dev = codec_dev;
437 priv->quirk = (unsigned long)dmi_id->driver_data;
438 acp_drvdata->mach_priv = priv;
439 dev_info(dev, "successfully probed the sound card\n");
440 } else {
441 ret = -ENODEV;
442 dev_warn(dev, "this system has a ES83xx codec defined in ACPI, but the driver doesn't have this system registered in DMI table\n");
443 }
444 return ret;
445 }
446
447
448 void acp3x_es83xx_init_ops(struct acp_mach_ops *ops)
449 {
450 ops->probe = acp3x_es83xx_probe;
451 ops->configure_widgets = acp3x_es83xx_configure_widgets;
452 ops->configure_link = acp3x_es83xx_configure_link;
453 ops->suspend_pre = acp3x_es83xx_suspend_pre;
454 ops->resume_post = acp3x_es83xx_resume_post;
455 }