blob: f21df28bc28e24eafc744fb8b524c26e9cbbd844 [file] [log] [blame]
Vinod Koul95f09802015-11-05 21:34:11 +05301/*
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -05002 * soc-apci.c - support for ACPI enumeration.
Vinod Koul95f09802015-11-05 21:34:11 +05303 *
4 * Copyright (c) 2013-15, Intel Corporation.
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 */
Vinod Koul95f09802015-11-05 21:34:11 +053016
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050017#include <sound/soc-acpi.h>
Vinod Koul95f09802015-11-05 21:34:11 +053018
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050019static acpi_status snd_soc_acpi_find_name(acpi_handle handle, u32 level,
Pierre-Louis Bossart1fdb7c12016-03-03 21:36:36 -060020 void *context, void **ret)
21{
22 struct acpi_device *adev;
23 const char *name = NULL;
24
25 if (acpi_bus_get_device(handle, &adev))
26 return AE_OK;
27
28 if (adev->status.present && adev->status.functional) {
29 name = acpi_dev_name(adev);
30 *(const char **)ret = name;
31 return AE_CTRL_TERMINATE;
32 }
33
34 return AE_OK;
35}
36
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050037const char *snd_soc_acpi_find_name_from_hid(const u8 hid[ACPI_ID_LEN])
Pierre-Louis Bossart1fdb7c12016-03-03 21:36:36 -060038{
39 const char *name = NULL;
40 acpi_status status;
41
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050042 status = acpi_get_devices(hid, snd_soc_acpi_find_name, NULL,
Pierre-Louis Bossart1fdb7c12016-03-03 21:36:36 -060043 (void **)&name);
44
45 if (ACPI_FAILURE(status) || name[0] == '\0')
46 return NULL;
47
48 return name;
49}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050050EXPORT_SYMBOL_GPL(snd_soc_acpi_find_name_from_hid);
Pierre-Louis Bossart1fdb7c12016-03-03 21:36:36 -060051
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050052static acpi_status snd_soc_acpi_mach_match(acpi_handle handle, u32 level,
Vinod Koul95f09802015-11-05 21:34:11 +053053 void *context, void **ret)
54{
Pierre-Louis Bossartcab47382016-03-03 21:36:34 -060055 unsigned long long sta;
56 acpi_status status;
57
Vinod Koul95f09802015-11-05 21:34:11 +053058 *(bool *)context = true;
Pierre-Louis Bossartcab47382016-03-03 21:36:34 -060059 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
60 if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_PRESENT))
61 *(bool *)context = false;
62
Vinod Koul95f09802015-11-05 21:34:11 +053063 return AE_OK;
64}
65
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050066bool snd_soc_acpi_check_hid(const u8 hid[ACPI_ID_LEN])
Naveen M915ae2b2017-05-15 13:42:13 +053067{
68 acpi_status status;
69 bool found = false;
70
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050071 status = acpi_get_devices(hid, snd_soc_acpi_mach_match, &found, NULL);
Naveen M915ae2b2017-05-15 13:42:13 +053072
73 if (ACPI_FAILURE(status))
74 return false;
75
76 return found;
77}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050078EXPORT_SYMBOL_GPL(snd_soc_acpi_check_hid);
Naveen M915ae2b2017-05-15 13:42:13 +053079
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050080struct snd_soc_acpi_mach *
81snd_soc_acpi_find_machine(struct snd_soc_acpi_mach *machines)
Vinod Koul95f09802015-11-05 21:34:11 +053082{
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050083 struct snd_soc_acpi_mach *mach;
Vinod Koul95f09802015-11-05 21:34:11 +053084
Naveen M7827d662017-05-15 13:42:14 +053085 for (mach = machines; mach->id[0]; mach++) {
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050086 if (snd_soc_acpi_check_hid(mach->id) == true) {
Naveen M7827d662017-05-15 13:42:14 +053087 if (mach->machine_quirk == NULL)
88 return mach;
89
90 if (mach->machine_quirk(mach) != NULL)
91 return mach;
92 }
93 }
Vinod Koul95f09802015-11-05 21:34:11 +053094 return NULL;
95}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050096EXPORT_SYMBOL_GPL(snd_soc_acpi_find_machine);
Vinod Koul8ceffd22016-02-08 10:45:39 +053097
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050098static acpi_status snd_soc_acpi_find_package(acpi_handle handle, u32 level,
99 void *context, void **ret)
Pierre-Louis Bossart34218942016-11-12 18:07:44 -0600100{
101 struct acpi_device *adev;
102 acpi_status status = AE_OK;
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500103 struct snd_soc_acpi_package_context *pkg_ctx = context;
Pierre-Louis Bossart34218942016-11-12 18:07:44 -0600104
105 pkg_ctx->data_valid = false;
106
107 if (acpi_bus_get_device(handle, &adev))
108 return AE_OK;
109
110 if (adev->status.present && adev->status.functional) {
111 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
112 union acpi_object *myobj = NULL;
113
114 status = acpi_evaluate_object_typed(handle, pkg_ctx->name,
115 NULL, &buffer,
116 ACPI_TYPE_PACKAGE);
117 if (ACPI_FAILURE(status))
118 return AE_OK;
119
120 myobj = buffer.pointer;
121 if (!myobj || myobj->package.count != pkg_ctx->length) {
122 kfree(buffer.pointer);
123 return AE_OK;
124 }
125
126 status = acpi_extract_package(myobj,
127 pkg_ctx->format, pkg_ctx->state);
128 if (ACPI_FAILURE(status)) {
129 kfree(buffer.pointer);
130 return AE_OK;
131 }
132
133 kfree(buffer.pointer);
134 pkg_ctx->data_valid = true;
135 return AE_CTRL_TERMINATE;
136 }
137
138 return AE_OK;
139}
140
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500141bool snd_soc_acpi_find_package_from_hid(const u8 hid[ACPI_ID_LEN],
142 struct snd_soc_acpi_package_context *ctx)
Pierre-Louis Bossart34218942016-11-12 18:07:44 -0600143{
144 acpi_status status;
145
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500146 status = acpi_get_devices(hid, snd_soc_acpi_find_package, ctx, NULL);
Pierre-Louis Bossart34218942016-11-12 18:07:44 -0600147
148 if (ACPI_FAILURE(status) || !ctx->data_valid)
149 return false;
150
151 return true;
152}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500153EXPORT_SYMBOL_GPL(snd_soc_acpi_find_package_from_hid);
Pierre-Louis Bossart34218942016-11-12 18:07:44 -0600154
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500155struct snd_soc_acpi_mach *snd_soc_acpi_codec_list(void *arg)
Naveen M54746da2017-05-15 13:42:15 +0530156{
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500157 struct snd_soc_acpi_mach *mach = arg;
158 struct snd_soc_acpi_codecs *codec_list =
159 (struct snd_soc_acpi_codecs *) mach->quirk_data;
Naveen M54746da2017-05-15 13:42:15 +0530160 int i;
161
162 if (mach->quirk_data == NULL)
163 return mach;
164
165 for (i = 0; i < codec_list->num_codecs; i++) {
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500166 if (snd_soc_acpi_check_hid(codec_list->codecs[i]) != true)
Naveen M54746da2017-05-15 13:42:15 +0530167 return NULL;
168 }
169
170 return mach;
171}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500172EXPORT_SYMBOL_GPL(snd_soc_acpi_codec_list);
Naveen M54746da2017-05-15 13:42:15 +0530173
Vinod Koul8ceffd22016-02-08 10:45:39 +0530174MODULE_LICENSE("GPL v2");
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500175MODULE_DESCRIPTION("ALSA SoC ACPI module");