blob: 444ce0602f7613bdee68167484bef51c8c615117 [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
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050011struct snd_soc_acpi_mach *
12snd_soc_acpi_find_machine(struct snd_soc_acpi_mach *machines)
Vinod Koul95f09802015-11-05 21:34:11 +053013{
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050014 struct snd_soc_acpi_mach *mach;
Keyon Jiea3e620f2018-11-16 18:47:04 -060015 struct snd_soc_acpi_mach *mach_alt;
Vinod Koul95f09802015-11-05 21:34:11 +053016
Naveen M7827d662017-05-15 13:42:14 +053017 for (mach = machines; mach->id[0]; mach++) {
Jeremy Cline0d5ea122018-01-05 14:55:34 -060018 if (acpi_dev_present(mach->id, NULL, -1)) {
Keyon Jiea3e620f2018-11-16 18:47:04 -060019 if (mach->machine_quirk) {
20 mach_alt = mach->machine_quirk(mach);
21 if (!mach_alt)
22 continue; /* not full match, ignore */
23 mach = mach_alt;
24 }
25
Pierre-Louis Bossart5c256042018-01-05 14:55:33 -060026 return mach;
Naveen M7827d662017-05-15 13:42:14 +053027 }
28 }
Vinod Koul95f09802015-11-05 21:34:11 +053029 return NULL;
30}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050031EXPORT_SYMBOL_GPL(snd_soc_acpi_find_machine);
Vinod Koul8ceffd22016-02-08 10:45:39 +053032
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050033static acpi_status snd_soc_acpi_find_package(acpi_handle handle, u32 level,
34 void *context, void **ret)
Pierre-Louis Bossart34218942016-11-12 18:07:44 -060035{
36 struct acpi_device *adev;
37 acpi_status status = AE_OK;
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050038 struct snd_soc_acpi_package_context *pkg_ctx = context;
Pierre-Louis Bossart34218942016-11-12 18:07:44 -060039
40 pkg_ctx->data_valid = false;
41
42 if (acpi_bus_get_device(handle, &adev))
43 return AE_OK;
44
45 if (adev->status.present && adev->status.functional) {
46 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
47 union acpi_object *myobj = NULL;
48
49 status = acpi_evaluate_object_typed(handle, pkg_ctx->name,
50 NULL, &buffer,
51 ACPI_TYPE_PACKAGE);
52 if (ACPI_FAILURE(status))
53 return AE_OK;
54
55 myobj = buffer.pointer;
56 if (!myobj || myobj->package.count != pkg_ctx->length) {
57 kfree(buffer.pointer);
58 return AE_OK;
59 }
60
61 status = acpi_extract_package(myobj,
62 pkg_ctx->format, pkg_ctx->state);
63 if (ACPI_FAILURE(status)) {
64 kfree(buffer.pointer);
65 return AE_OK;
66 }
67
68 kfree(buffer.pointer);
69 pkg_ctx->data_valid = true;
70 return AE_CTRL_TERMINATE;
71 }
72
73 return AE_OK;
74}
75
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050076bool snd_soc_acpi_find_package_from_hid(const u8 hid[ACPI_ID_LEN],
77 struct snd_soc_acpi_package_context *ctx)
Pierre-Louis Bossart34218942016-11-12 18:07:44 -060078{
79 acpi_status status;
80
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050081 status = acpi_get_devices(hid, snd_soc_acpi_find_package, ctx, NULL);
Pierre-Louis Bossart34218942016-11-12 18:07:44 -060082
83 if (ACPI_FAILURE(status) || !ctx->data_valid)
84 return false;
85
86 return true;
87}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050088EXPORT_SYMBOL_GPL(snd_soc_acpi_find_package_from_hid);
Pierre-Louis Bossart34218942016-11-12 18:07:44 -060089
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050090struct snd_soc_acpi_mach *snd_soc_acpi_codec_list(void *arg)
Naveen M54746da2017-05-15 13:42:15 +053091{
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050092 struct snd_soc_acpi_mach *mach = arg;
93 struct snd_soc_acpi_codecs *codec_list =
94 (struct snd_soc_acpi_codecs *) mach->quirk_data;
Naveen M54746da2017-05-15 13:42:15 +053095 int i;
96
97 if (mach->quirk_data == NULL)
98 return mach;
99
100 for (i = 0; i < codec_list->num_codecs; i++) {
Jeremy Cline0d5ea122018-01-05 14:55:34 -0600101 if (!acpi_dev_present(codec_list->codecs[i], NULL, -1))
Naveen M54746da2017-05-15 13:42:15 +0530102 return NULL;
103 }
104
105 return mach;
106}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500107EXPORT_SYMBOL_GPL(snd_soc_acpi_codec_list);
Naveen M54746da2017-05-15 13:42:15 +0530108
Vinod Koul8ceffd22016-02-08 10:45:39 +0530109MODULE_LICENSE("GPL v2");
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500110MODULE_DESCRIPTION("ALSA SoC ACPI module");