blob: 7f43c9bf3d09054a2893d6478c38150f71c49534 [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 -050052struct snd_soc_acpi_mach *
53snd_soc_acpi_find_machine(struct snd_soc_acpi_mach *machines)
Vinod Koul95f09802015-11-05 21:34:11 +053054{
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050055 struct snd_soc_acpi_mach *mach;
Vinod Koul95f09802015-11-05 21:34:11 +053056
Naveen M7827d662017-05-15 13:42:14 +053057 for (mach = machines; mach->id[0]; mach++) {
Jeremy Cline0d5ea122018-01-05 14:55:34 -060058 if (acpi_dev_present(mach->id, NULL, -1)) {
Pierre-Louis Bossart5c256042018-01-05 14:55:33 -060059 if (mach->machine_quirk)
60 mach = mach->machine_quirk(mach);
61 return mach;
Naveen M7827d662017-05-15 13:42:14 +053062 }
63 }
Vinod Koul95f09802015-11-05 21:34:11 +053064 return NULL;
65}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050066EXPORT_SYMBOL_GPL(snd_soc_acpi_find_machine);
Vinod Koul8ceffd22016-02-08 10:45:39 +053067
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050068static acpi_status snd_soc_acpi_find_package(acpi_handle handle, u32 level,
69 void *context, void **ret)
Pierre-Louis Bossart34218942016-11-12 18:07:44 -060070{
71 struct acpi_device *adev;
72 acpi_status status = AE_OK;
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -050073 struct snd_soc_acpi_package_context *pkg_ctx = context;
Pierre-Louis Bossart34218942016-11-12 18:07:44 -060074
75 pkg_ctx->data_valid = false;
76
77 if (acpi_bus_get_device(handle, &adev))
78 return AE_OK;
79
80 if (adev->status.present && adev->status.functional) {
81 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
82 union acpi_object *myobj = NULL;
83
84 status = acpi_evaluate_object_typed(handle, pkg_ctx->name,
85 NULL, &buffer,
86 ACPI_TYPE_PACKAGE);
87 if (ACPI_FAILURE(status))
88 return AE_OK;
89
90 myobj = buffer.pointer;
91 if (!myobj || myobj->package.count != pkg_ctx->length) {
92 kfree(buffer.pointer);
93 return AE_OK;
94 }
95
96 status = acpi_extract_package(myobj,
97 pkg_ctx->format, pkg_ctx->state);
98 if (ACPI_FAILURE(status)) {
99 kfree(buffer.pointer);
100 return AE_OK;
101 }
102
103 kfree(buffer.pointer);
104 pkg_ctx->data_valid = true;
105 return AE_CTRL_TERMINATE;
106 }
107
108 return AE_OK;
109}
110
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500111bool snd_soc_acpi_find_package_from_hid(const u8 hid[ACPI_ID_LEN],
112 struct snd_soc_acpi_package_context *ctx)
Pierre-Louis Bossart34218942016-11-12 18:07:44 -0600113{
114 acpi_status status;
115
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500116 status = acpi_get_devices(hid, snd_soc_acpi_find_package, ctx, NULL);
Pierre-Louis Bossart34218942016-11-12 18:07:44 -0600117
118 if (ACPI_FAILURE(status) || !ctx->data_valid)
119 return false;
120
121 return true;
122}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500123EXPORT_SYMBOL_GPL(snd_soc_acpi_find_package_from_hid);
Pierre-Louis Bossart34218942016-11-12 18:07:44 -0600124
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500125struct snd_soc_acpi_mach *snd_soc_acpi_codec_list(void *arg)
Naveen M54746da2017-05-15 13:42:15 +0530126{
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500127 struct snd_soc_acpi_mach *mach = arg;
128 struct snd_soc_acpi_codecs *codec_list =
129 (struct snd_soc_acpi_codecs *) mach->quirk_data;
Naveen M54746da2017-05-15 13:42:15 +0530130 int i;
131
132 if (mach->quirk_data == NULL)
133 return mach;
134
135 for (i = 0; i < codec_list->num_codecs; i++) {
Jeremy Cline0d5ea122018-01-05 14:55:34 -0600136 if (!acpi_dev_present(codec_list->codecs[i], NULL, -1))
Naveen M54746da2017-05-15 13:42:15 +0530137 return NULL;
138 }
139
140 return mach;
141}
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500142EXPORT_SYMBOL_GPL(snd_soc_acpi_codec_list);
Naveen M54746da2017-05-15 13:42:15 +0530143
Vinod Koul8ceffd22016-02-08 10:45:39 +0530144MODULE_LICENSE("GPL v2");
Pierre-Louis Bossart7feb2f72017-10-12 18:49:38 -0500145MODULE_DESCRIPTION("ALSA SoC ACPI module");