blob: 142476f1396ff42646728906423a429f38439160 [file] [log] [blame]
Kuninori Morimoto7730bb12018-07-02 06:24:04 +00001// SPDX-License-Identifier: GPL-2.0
2//
3// soc-apci.c - support for ACPI enumeration.
4//
5// Copyright (c) 2013-15, Intel Corporation.
Vinod Koul95f09802015-11-05 21:34:11 +05306
Paul Gortmaker261e9082019-04-13 11:15:18 -04007#include <linux/export.h>
8#include <linux/module.h>
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -05009#include <sound/soc-acpi.h>
Vinod Koul95f09802015-11-05 21:34:11 +053010
Brent Lucafa39b2021-10-30 01:14:04 +080011static bool snd_soc_acpi_id_present(struct snd_soc_acpi_mach *machine)
12{
13 const struct snd_soc_acpi_codecs *comp_ids = machine->comp_ids;
14 int i;
15
16 if (machine->id[0]) {
17 if (acpi_dev_present(machine->id, NULL, -1))
18 return true;
19 }
20
21 if (comp_ids) {
22 for (i = 0; i < comp_ids->num_codecs; i++) {
Hans de Goede28c916a2021-11-18 16:30:14 +010023 if (acpi_dev_present(comp_ids->codecs[i], NULL, -1)) {
24 strscpy(machine->id, comp_ids->codecs[i], ACPI_ID_LEN);
Brent Lucafa39b2021-10-30 01:14:04 +080025 return true;
Hans de Goede28c916a2021-11-18 16:30:14 +010026 }
Brent Lucafa39b2021-10-30 01:14:04 +080027 }
28 }
29
30 return false;
31}
32
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050033struct snd_soc_acpi_mach *
34snd_soc_acpi_find_machine(struct snd_soc_acpi_mach *machines)
Vinod Koul95f09802015-11-05 21:34:11 +053035{
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050036 struct snd_soc_acpi_mach *mach;
Keyon Jiea3e620f2018-11-16 18:47:04 -060037 struct snd_soc_acpi_mach *mach_alt;
Vinod Koul95f09802015-11-05 21:34:11 +053038
Brent Lucafa39b2021-10-30 01:14:04 +080039 for (mach = machines; mach->id[0] || mach->comp_ids; mach++) {
40 if (snd_soc_acpi_id_present(mach)) {
Keyon Jiea3e620f2018-11-16 18:47:04 -060041 if (mach->machine_quirk) {
42 mach_alt = mach->machine_quirk(mach);
43 if (!mach_alt)
44 continue; /* not full match, ignore */
45 mach = mach_alt;
46 }
47
Pierre-Louis Bossart5c256042018-01-05 14:55:33 -060048 return mach;
Naveen M7827d662017-05-15 13:42:14 +053049 }
50 }
Vinod Koul95f09802015-11-05 21:34:11 +053051 return NULL;
52}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050053EXPORT_SYMBOL_GPL(snd_soc_acpi_find_machine);
Vinod Koul8ceffd22016-02-08 10:45:39 +053054
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050055static acpi_status snd_soc_acpi_find_package(acpi_handle handle, u32 level,
56 void *context, void **ret)
Pierre-Louis Bossart34218942016-11-12 18:07:44 -060057{
Rafael J. Wysockiff4865b2022-01-26 20:48:49 +010058 struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
Pierre-Louis Bossart59ce3232021-04-16 14:11:40 -050059 acpi_status status;
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050060 struct snd_soc_acpi_package_context *pkg_ctx = context;
Pierre-Louis Bossart34218942016-11-12 18:07:44 -060061
62 pkg_ctx->data_valid = false;
63
Rafael J. Wysockiff4865b2022-01-26 20:48:49 +010064 if (adev && adev->status.present && adev->status.functional) {
Pierre-Louis Bossart34218942016-11-12 18:07:44 -060065 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
66 union acpi_object *myobj = NULL;
67
68 status = acpi_evaluate_object_typed(handle, pkg_ctx->name,
69 NULL, &buffer,
70 ACPI_TYPE_PACKAGE);
71 if (ACPI_FAILURE(status))
72 return AE_OK;
73
74 myobj = buffer.pointer;
75 if (!myobj || myobj->package.count != pkg_ctx->length) {
76 kfree(buffer.pointer);
77 return AE_OK;
78 }
79
80 status = acpi_extract_package(myobj,
81 pkg_ctx->format, pkg_ctx->state);
82 if (ACPI_FAILURE(status)) {
83 kfree(buffer.pointer);
84 return AE_OK;
85 }
86
87 kfree(buffer.pointer);
88 pkg_ctx->data_valid = true;
89 return AE_CTRL_TERMINATE;
90 }
91
92 return AE_OK;
93}
94
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050095bool snd_soc_acpi_find_package_from_hid(const u8 hid[ACPI_ID_LEN],
96 struct snd_soc_acpi_package_context *ctx)
Pierre-Louis Bossart34218942016-11-12 18:07:44 -060097{
98 acpi_status status;
99
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500100 status = acpi_get_devices(hid, snd_soc_acpi_find_package, ctx, NULL);
Pierre-Louis Bossart34218942016-11-12 18:07:44 -0600101
102 if (ACPI_FAILURE(status) || !ctx->data_valid)
103 return false;
104
105 return true;
106}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500107EXPORT_SYMBOL_GPL(snd_soc_acpi_find_package_from_hid);
Pierre-Louis Bossart34218942016-11-12 18:07:44 -0600108
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500109struct snd_soc_acpi_mach *snd_soc_acpi_codec_list(void *arg)
Naveen M54746da2017-05-15 13:42:15 +0530110{
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500111 struct snd_soc_acpi_mach *mach = arg;
112 struct snd_soc_acpi_codecs *codec_list =
113 (struct snd_soc_acpi_codecs *) mach->quirk_data;
Naveen M54746da2017-05-15 13:42:15 +0530114 int i;
115
116 if (mach->quirk_data == NULL)
117 return mach;
118
119 for (i = 0; i < codec_list->num_codecs; i++) {
Jeremy Cline0d5ea122018-01-05 14:55:34 -0600120 if (!acpi_dev_present(codec_list->codecs[i], NULL, -1))
Naveen M54746da2017-05-15 13:42:15 +0530121 return NULL;
122 }
123
124 return mach;
125}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500126EXPORT_SYMBOL_GPL(snd_soc_acpi_codec_list);
Naveen M54746da2017-05-15 13:42:15 +0530127
Vinod Koul8ceffd22016-02-08 10:45:39 +0530128MODULE_LICENSE("GPL v2");
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500129MODULE_DESCRIPTION("ALSA SoC ACPI module");