Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 1 | /* |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 2 | * soc-apci.c - support for ACPI enumeration. |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 3 | * |
| 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 Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 16 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 17 | #include <sound/soc-acpi.h> |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 18 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 19 | static acpi_status snd_soc_acpi_find_name(acpi_handle handle, u32 level, |
Pierre-Louis Bossart | 1fdb7c1 | 2016-03-03 21:36:36 -0600 | [diff] [blame] | 20 | 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 Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 37 | const char *snd_soc_acpi_find_name_from_hid(const u8 hid[ACPI_ID_LEN]) |
Pierre-Louis Bossart | 1fdb7c1 | 2016-03-03 21:36:36 -0600 | [diff] [blame] | 38 | { |
| 39 | const char *name = NULL; |
| 40 | acpi_status status; |
| 41 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 42 | status = acpi_get_devices(hid, snd_soc_acpi_find_name, NULL, |
Pierre-Louis Bossart | 1fdb7c1 | 2016-03-03 21:36:36 -0600 | [diff] [blame] | 43 | (void **)&name); |
| 44 | |
| 45 | if (ACPI_FAILURE(status) || name[0] == '\0') |
| 46 | return NULL; |
| 47 | |
| 48 | return name; |
| 49 | } |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 50 | EXPORT_SYMBOL_GPL(snd_soc_acpi_find_name_from_hid); |
Pierre-Louis Bossart | 1fdb7c1 | 2016-03-03 21:36:36 -0600 | [diff] [blame] | 51 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 52 | static acpi_status snd_soc_acpi_mach_match(acpi_handle handle, u32 level, |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 53 | void *context, void **ret) |
| 54 | { |
Pierre-Louis Bossart | cab4738 | 2016-03-03 21:36:34 -0600 | [diff] [blame] | 55 | unsigned long long sta; |
| 56 | acpi_status status; |
| 57 | |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 58 | *(bool *)context = true; |
Pierre-Louis Bossart | cab4738 | 2016-03-03 21:36:34 -0600 | [diff] [blame] | 59 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
| 60 | if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_PRESENT)) |
| 61 | *(bool *)context = false; |
| 62 | |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 63 | return AE_OK; |
| 64 | } |
| 65 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 66 | bool snd_soc_acpi_check_hid(const u8 hid[ACPI_ID_LEN]) |
Naveen M | 915ae2b | 2017-05-15 13:42:13 +0530 | [diff] [blame] | 67 | { |
| 68 | acpi_status status; |
| 69 | bool found = false; |
| 70 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 71 | status = acpi_get_devices(hid, snd_soc_acpi_mach_match, &found, NULL); |
Naveen M | 915ae2b | 2017-05-15 13:42:13 +0530 | [diff] [blame] | 72 | |
| 73 | if (ACPI_FAILURE(status)) |
| 74 | return false; |
| 75 | |
| 76 | return found; |
| 77 | } |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 78 | EXPORT_SYMBOL_GPL(snd_soc_acpi_check_hid); |
Naveen M | 915ae2b | 2017-05-15 13:42:13 +0530 | [diff] [blame] | 79 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 80 | struct snd_soc_acpi_mach * |
| 81 | snd_soc_acpi_find_machine(struct snd_soc_acpi_mach *machines) |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 82 | { |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 83 | struct snd_soc_acpi_mach *mach; |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 84 | |
Naveen M | 7827d66 | 2017-05-15 13:42:14 +0530 | [diff] [blame] | 85 | for (mach = machines; mach->id[0]; mach++) { |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 86 | if (snd_soc_acpi_check_hid(mach->id) == true) { |
Pierre-Louis Bossart | 5c25604 | 2018-01-05 14:55:33 -0600 | [diff] [blame^] | 87 | if (mach->machine_quirk) |
| 88 | mach = mach->machine_quirk(mach); |
| 89 | return mach; |
Naveen M | 7827d66 | 2017-05-15 13:42:14 +0530 | [diff] [blame] | 90 | } |
| 91 | } |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 94 | EXPORT_SYMBOL_GPL(snd_soc_acpi_find_machine); |
Vinod Koul | 8ceffd2 | 2016-02-08 10:45:39 +0530 | [diff] [blame] | 95 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 96 | static acpi_status snd_soc_acpi_find_package(acpi_handle handle, u32 level, |
| 97 | void *context, void **ret) |
Pierre-Louis Bossart | 3421894 | 2016-11-12 18:07:44 -0600 | [diff] [blame] | 98 | { |
| 99 | struct acpi_device *adev; |
| 100 | acpi_status status = AE_OK; |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 101 | struct snd_soc_acpi_package_context *pkg_ctx = context; |
Pierre-Louis Bossart | 3421894 | 2016-11-12 18:07:44 -0600 | [diff] [blame] | 102 | |
| 103 | pkg_ctx->data_valid = false; |
| 104 | |
| 105 | if (acpi_bus_get_device(handle, &adev)) |
| 106 | return AE_OK; |
| 107 | |
| 108 | if (adev->status.present && adev->status.functional) { |
| 109 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 110 | union acpi_object *myobj = NULL; |
| 111 | |
| 112 | status = acpi_evaluate_object_typed(handle, pkg_ctx->name, |
| 113 | NULL, &buffer, |
| 114 | ACPI_TYPE_PACKAGE); |
| 115 | if (ACPI_FAILURE(status)) |
| 116 | return AE_OK; |
| 117 | |
| 118 | myobj = buffer.pointer; |
| 119 | if (!myobj || myobj->package.count != pkg_ctx->length) { |
| 120 | kfree(buffer.pointer); |
| 121 | return AE_OK; |
| 122 | } |
| 123 | |
| 124 | status = acpi_extract_package(myobj, |
| 125 | pkg_ctx->format, pkg_ctx->state); |
| 126 | if (ACPI_FAILURE(status)) { |
| 127 | kfree(buffer.pointer); |
| 128 | return AE_OK; |
| 129 | } |
| 130 | |
| 131 | kfree(buffer.pointer); |
| 132 | pkg_ctx->data_valid = true; |
| 133 | return AE_CTRL_TERMINATE; |
| 134 | } |
| 135 | |
| 136 | return AE_OK; |
| 137 | } |
| 138 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 139 | bool snd_soc_acpi_find_package_from_hid(const u8 hid[ACPI_ID_LEN], |
| 140 | struct snd_soc_acpi_package_context *ctx) |
Pierre-Louis Bossart | 3421894 | 2016-11-12 18:07:44 -0600 | [diff] [blame] | 141 | { |
| 142 | acpi_status status; |
| 143 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 144 | status = acpi_get_devices(hid, snd_soc_acpi_find_package, ctx, NULL); |
Pierre-Louis Bossart | 3421894 | 2016-11-12 18:07:44 -0600 | [diff] [blame] | 145 | |
| 146 | if (ACPI_FAILURE(status) || !ctx->data_valid) |
| 147 | return false; |
| 148 | |
| 149 | return true; |
| 150 | } |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 151 | EXPORT_SYMBOL_GPL(snd_soc_acpi_find_package_from_hid); |
Pierre-Louis Bossart | 3421894 | 2016-11-12 18:07:44 -0600 | [diff] [blame] | 152 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 153 | struct snd_soc_acpi_mach *snd_soc_acpi_codec_list(void *arg) |
Naveen M | 54746da | 2017-05-15 13:42:15 +0530 | [diff] [blame] | 154 | { |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 155 | struct snd_soc_acpi_mach *mach = arg; |
| 156 | struct snd_soc_acpi_codecs *codec_list = |
| 157 | (struct snd_soc_acpi_codecs *) mach->quirk_data; |
Naveen M | 54746da | 2017-05-15 13:42:15 +0530 | [diff] [blame] | 158 | int i; |
| 159 | |
| 160 | if (mach->quirk_data == NULL) |
| 161 | return mach; |
| 162 | |
| 163 | for (i = 0; i < codec_list->num_codecs; i++) { |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 164 | if (snd_soc_acpi_check_hid(codec_list->codecs[i]) != true) |
Naveen M | 54746da | 2017-05-15 13:42:15 +0530 | [diff] [blame] | 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | return mach; |
| 169 | } |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 170 | EXPORT_SYMBOL_GPL(snd_soc_acpi_codec_list); |
Naveen M | 54746da | 2017-05-15 13:42:15 +0530 | [diff] [blame] | 171 | |
Vinod Koul | 8ceffd2 | 2016-02-08 10:45:39 +0530 | [diff] [blame] | 172 | MODULE_LICENSE("GPL v2"); |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 173 | MODULE_DESCRIPTION("ALSA SoC ACPI module"); |