1 // SPDX-License-Identifier: GPL-2.0
3 * max98357a.c -- MAX98357A Audio driver
5 * Copyright 2019 Google LLC
6 * Parts taken from coreboot
10 #include <audio_codec.h>
14 #include <acpi/acpigen.h>
15 #include <acpi/acpi_device.h>
16 #include <acpi/acpi_dp.h>
17 #include <asm-generic/gpio.h>
19 #include <asm/acpi_nhlt.h>
21 #include <dt-bindings/sound/nhlt.h>
24 struct max98357a_priv {
25 struct gpio_desc sdmode_gpio;
28 static int max98357a_of_to_plat(struct udevice *dev)
30 struct max98357a_priv *priv = dev_get_priv(dev);
33 ret = gpio_request_by_name(dev, "sdmode-gpios", 0, &priv->sdmode_gpio,
36 return log_msg_ret("gpio", ret);
41 static int max98357a_acpi_fill_ssdt(const struct udevice *dev,
44 struct max98357a_priv *priv = dev_get_priv(dev);
45 char scope[ACPI_PATH_MAX];
46 char name[ACPI_NAME_MAX];
47 char path[ACPI_PATH_MAX];
51 ret = acpi_device_scope(dev, scope, sizeof(scope));
53 return log_msg_ret("scope", ret);
54 ret = acpi_get_name(dev, name);
56 return log_msg_ret("name", ret);
59 acpigen_write_scope(ctx, scope);
60 acpigen_write_device(ctx, name);
61 acpigen_write_name_string(ctx, "_HID",
62 dev_read_string(dev, "acpi,hid"));
63 acpigen_write_name_integer(ctx, "_UID", 0);
64 acpigen_write_name_string(ctx, "_DDN",
65 dev_read_string(dev, "acpi,ddn"));
66 acpigen_write_sta(ctx, acpi_device_status(dev));
69 acpigen_write_name(ctx, "_CRS");
70 acpigen_write_resourcetemplate_header(ctx);
71 ret = acpi_device_write_gpio_desc(ctx, &priv->sdmode_gpio);
73 return log_msg_ret("gpio", ret);
74 acpigen_write_resourcetemplate_footer(ctx);
76 /* _DSD for devicetree properties */
77 /* This points to the first pin in the first gpio entry in _CRS */
78 ret = acpi_device_path(dev, path, sizeof(path));
80 return log_msg_ret("path", ret);
81 dp = acpi_dp_new_table("_DSD");
82 acpi_dp_add_gpio(dp, "sdmode-gpio", path, 0, 0,
83 priv->sdmode_gpio.flags & GPIOD_ACTIVE_LOW ?
84 ACPI_GPIO_ACTIVE_LOW : ACPI_GPIO_ACTIVE_HIGH);
85 acpi_dp_add_integer(dp, "sdmode-delay",
86 dev_read_u32_default(dev, "sdmode-delay", 0));
87 acpi_dp_write(ctx, dp);
89 acpigen_pop_len(ctx); /* Device */
90 acpigen_pop_len(ctx); /* Scope */
95 /* For now only X86 boards support NHLT */
97 static const struct nhlt_format_config max98357a_formats[] = {
98 /* 48 KHz 24-bits per sample. */
101 .sample_freq_khz = 48,
102 .container_bits_per_sample = 32,
103 .valid_bits_per_sample = 24,
104 .settings_file = "max98357-render-2ch-48khz-24b.dat",
108 static const struct nhlt_endp_descriptor max98357a_descriptors[] = {
110 .link = NHLT_LINK_SSP,
111 .device = NHLT_SSP_DEV_I2S,
112 .direction = NHLT_DIR_RENDER,
115 .formats = max98357a_formats,
116 .num_formats = ARRAY_SIZE(max98357a_formats),
120 static int max98357a_acpi_setup_nhlt(const struct udevice *dev,
121 struct acpi_ctx *ctx)
126 if (dev_read_u32(dev, "acpi,audio-link", &hwlink))
127 return log_msg_ret("link", -EINVAL);
129 /* Virtual bus id of SSP links are the hardware port ids proper. */
130 ret = nhlt_add_ssp_endpoints(ctx->nhlt, hwlink, max98357a_descriptors,
131 ARRAY_SIZE(max98357a_descriptors));
133 return log_msg_ret("add", ret);
139 struct acpi_ops max98357a_acpi_ops = {
140 .fill_ssdt = max98357a_acpi_fill_ssdt,
142 .setup_nhlt = max98357a_acpi_setup_nhlt,
146 static const struct audio_codec_ops max98357a_ops = {
149 static const struct udevice_id max98357a_ids[] = {
150 { .compatible = "maxim,max98357a" },
154 U_BOOT_DRIVER(max98357a) = {
156 .id = UCLASS_AUDIO_CODEC,
157 .of_match = max98357a_ids,
158 .of_to_plat = max98357a_of_to_plat,
159 .ops = &max98357a_ops,
160 ACPI_OPS_PTR(&max98357a_acpi_ops)