summaryrefslogtreecommitdiff
path: root/drivers/sound/max98357a.c
blob: 841bc6ef682d02cb85d184f2646a3ac71aeacd05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// SPDX-License-Identifier: GPL-2.0
/*
 * max98357a.c -- MAX98357A Audio driver
 *
 * Copyright 2019 Google LLC
 * Parts taken from coreboot
 */

#include <common.h>
#include <audio_codec.h>
#include <dm.h>
#include <log.h>
#include <sound.h>
#include <acpi/acpigen.h>
#include <acpi/acpi_device.h>
#include <acpi/acpi_dp.h>
#include <asm-generic/gpio.h>
#ifdef CONFIG_X86
#include <asm/acpi_nhlt.h>
#endif
#include <dt-bindings/sound/nhlt.h>
#include <dm/acpi.h>

struct max98357a_priv {
	struct gpio_desc sdmode_gpio;
};

static int max98357a_ofdata_to_platdata(struct udevice *dev)
{
	struct max98357a_priv *priv = dev_get_priv(dev);
	int ret;

	ret = gpio_request_by_name(dev, "sdmode-gpios", 0, &priv->sdmode_gpio,
				   GPIOD_IS_IN);
	if (ret)
		return log_msg_ret("gpio", ret);

	return 0;
}

static int max98357a_acpi_fill_ssdt(const struct udevice *dev,
				    struct acpi_ctx *ctx)
{
	struct max98357a_priv *priv = dev_get_priv(dev);
	char scope[ACPI_PATH_MAX];
	char name[ACPI_NAME_MAX];
	char path[ACPI_PATH_MAX];
	struct acpi_dp *dp;
	int ret;

	ret = acpi_device_scope(dev, scope, sizeof(scope));
	if (ret)
		return log_msg_ret("scope", ret);
	ret = acpi_get_name(dev, name);
	if (ret)
		return log_msg_ret("name", ret);

	/* Device */
	acpigen_write_scope(ctx, scope);
	acpigen_write_device(ctx, name);
	acpigen_write_name_string(ctx, "_HID",
				  dev_read_string(dev, "acpi,hid"));
	acpigen_write_name_integer(ctx, "_UID", 0);
	acpigen_write_name_string(ctx, "_DDN",
				  dev_read_string(dev, "acpi,ddn"));
	acpigen_write_sta(ctx, acpi_device_status(dev));

	/* Resources */
	acpigen_write_name(ctx, "_CRS");
	acpigen_write_resourcetemplate_header(ctx);
	ret = acpi_device_write_gpio_desc(ctx, &priv->sdmode_gpio);
	if (ret)
		return log_msg_ret("gpio", ret);
	acpigen_write_resourcetemplate_footer(ctx);

	/* _DSD for devicetree properties */
	/* This points to the first pin in the first gpio entry in _CRS */
	ret = acpi_device_path(dev, path, sizeof(path));
	if (ret)
		return log_msg_ret("path", ret);
	dp = acpi_dp_new_table("_DSD");
	acpi_dp_add_gpio(dp, "sdmode-gpio", path, 0, 0,
			 priv->sdmode_gpio.flags & GPIOD_ACTIVE_LOW ?
			 ACPI_IRQ_ACTIVE_LOW : ACPI_IRQ_ACTIVE_HIGH);
	acpi_dp_add_integer(dp, "sdmode-delay",
			    dev_read_u32_default(dev, "sdmode-delay", 0));
	acpi_dp_write(ctx, dp);

	acpigen_pop_len(ctx); /* Device */
	acpigen_pop_len(ctx); /* Scope */

	return 0;
}

/* For now only X86 boards support NHLT */
#ifdef CONFIG_X86
static const struct nhlt_format_config max98357a_formats[] = {
	/* 48 KHz 24-bits per sample. */
	{
		.num_channels = 2,
		.sample_freq_khz = 48,
		.container_bits_per_sample = 32,
		.valid_bits_per_sample = 24,
		.settings_file = "max98357-render-2ch-48khz-24b.dat",
	},
};

static const struct nhlt_endp_descriptor max98357a_descriptors[] = {
	{
		.link = NHLT_LINK_SSP,
		.device = NHLT_SSP_DEV_I2S,
		.direction = NHLT_DIR_RENDER,
		.vid = NHLT_VID,
		.did = NHLT_DID_SSP,
		.formats = max98357a_formats,
		.num_formats = ARRAY_SIZE(max98357a_formats),
	},
};

static int max98357a_acpi_setup_nhlt(const struct udevice *dev,
				     struct acpi_ctx *ctx)
{
	u32 hwlink;
	int ret;

	if (dev_read_u32(dev, "acpi,audio-link", &hwlink))
		return log_msg_ret("link", -EINVAL);

	/* Virtual bus id of SSP links are the hardware port ids proper. */
	ret = nhlt_add_ssp_endpoints(ctx->nhlt, hwlink, max98357a_descriptors,
				     ARRAY_SIZE(max98357a_descriptors));
	if (ret)
		return log_msg_ret("add", ret);

	return 0;
}
#endif

struct acpi_ops max98357a_acpi_ops = {
	.fill_ssdt	= max98357a_acpi_fill_ssdt,
#ifdef CONFIG_X86
	.setup_nhlt	= max98357a_acpi_setup_nhlt,
#endif
};

static const struct audio_codec_ops max98357a_ops = {
};

static const struct udevice_id max98357a_ids[] = {
	{ .compatible = "maxim,max98357a" },
	{ }
};

U_BOOT_DRIVER(max98357a) = {
	.name		= "max98357a",
	.id		= UCLASS_AUDIO_CODEC,
	.of_match	= max98357a_ids,
	.ofdata_to_platdata	= max98357a_ofdata_to_platdata,
	.ops		= &max98357a_ops,
	ACPI_OPS_PTR(&max98357a_acpi_ops)
};