Kuninori Morimoto | 7730bb1 | 2018-07-02 06:24:04 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // soc-apci.c - support for ACPI enumeration. |
| 4 | // |
| 5 | // Copyright (c) 2013-15, Intel Corporation. |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 6 | |
Paul Gortmaker | 261e908 | 2019-04-13 11:15:18 -0400 | [diff] [blame] | 7 | #include <linux/export.h> |
| 8 | #include <linux/module.h> |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 9 | #include <sound/soc-acpi.h> |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 10 | |
Brent Lu | cafa39b | 2021-10-30 01:14:04 +0800 | [diff] [blame] | 11 | static 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 Goede | 28c916a | 2021-11-18 16:30:14 +0100 | [diff] [blame] | 23 | if (acpi_dev_present(comp_ids->codecs[i], NULL, -1)) { |
| 24 | strscpy(machine->id, comp_ids->codecs[i], ACPI_ID_LEN); |
Brent Lu | cafa39b | 2021-10-30 01:14:04 +0800 | [diff] [blame] | 25 | return true; |
Hans de Goede | 28c916a | 2021-11-18 16:30:14 +0100 | [diff] [blame] | 26 | } |
Brent Lu | cafa39b | 2021-10-30 01:14:04 +0800 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | |
| 30 | return false; |
| 31 | } |
| 32 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 33 | struct snd_soc_acpi_mach * |
| 34 | snd_soc_acpi_find_machine(struct snd_soc_acpi_mach *machines) |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 35 | { |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 36 | struct snd_soc_acpi_mach *mach; |
Keyon Jie | a3e620f | 2018-11-16 18:47:04 -0600 | [diff] [blame] | 37 | struct snd_soc_acpi_mach *mach_alt; |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 38 | |
Brent Lu | cafa39b | 2021-10-30 01:14:04 +0800 | [diff] [blame] | 39 | for (mach = machines; mach->id[0] || mach->comp_ids; mach++) { |
| 40 | if (snd_soc_acpi_id_present(mach)) { |
Keyon Jie | a3e620f | 2018-11-16 18:47:04 -0600 | [diff] [blame] | 41 | 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 Bossart | 5c25604 | 2018-01-05 14:55:33 -0600 | [diff] [blame] | 48 | return mach; |
Naveen M | 7827d66 | 2017-05-15 13:42:14 +0530 | [diff] [blame] | 49 | } |
| 50 | } |
Vinod Koul | 95f0980 | 2015-11-05 21:34:11 +0530 | [diff] [blame] | 51 | return NULL; |
| 52 | } |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 53 | EXPORT_SYMBOL_GPL(snd_soc_acpi_find_machine); |
Vinod Koul | 8ceffd2 | 2016-02-08 10:45:39 +0530 | [diff] [blame] | 54 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 55 | static acpi_status snd_soc_acpi_find_package(acpi_handle handle, u32 level, |
| 56 | void *context, void **ret) |
Pierre-Louis Bossart | 3421894 | 2016-11-12 18:07:44 -0600 | [diff] [blame] | 57 | { |
| 58 | struct acpi_device *adev; |
Pierre-Louis Bossart | 59ce323 | 2021-04-16 14:11:40 -0500 | [diff] [blame] | 59 | acpi_status status; |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 60 | struct snd_soc_acpi_package_context *pkg_ctx = context; |
Pierre-Louis Bossart | 3421894 | 2016-11-12 18:07:44 -0600 | [diff] [blame] | 61 | |
| 62 | pkg_ctx->data_valid = false; |
| 63 | |
| 64 | if (acpi_bus_get_device(handle, &adev)) |
| 65 | return AE_OK; |
| 66 | |
| 67 | if (adev->status.present && adev->status.functional) { |
| 68 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 69 | union acpi_object *myobj = NULL; |
| 70 | |
| 71 | status = acpi_evaluate_object_typed(handle, pkg_ctx->name, |
| 72 | NULL, &buffer, |
| 73 | ACPI_TYPE_PACKAGE); |
| 74 | if (ACPI_FAILURE(status)) |
| 75 | return AE_OK; |
| 76 | |
| 77 | myobj = buffer.pointer; |
| 78 | if (!myobj || myobj->package.count != pkg_ctx->length) { |
| 79 | kfree(buffer.pointer); |
| 80 | return AE_OK; |
| 81 | } |
| 82 | |
| 83 | status = acpi_extract_package(myobj, |
| 84 | pkg_ctx->format, pkg_ctx->state); |
| 85 | if (ACPI_FAILURE(status)) { |
| 86 | kfree(buffer.pointer); |
| 87 | return AE_OK; |
| 88 | } |
| 89 | |
| 90 | kfree(buffer.pointer); |
| 91 | pkg_ctx->data_valid = true; |
| 92 | return AE_CTRL_TERMINATE; |
| 93 | } |
| 94 | |
| 95 | return AE_OK; |
| 96 | } |
| 97 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 98 | bool snd_soc_acpi_find_package_from_hid(const u8 hid[ACPI_ID_LEN], |
| 99 | struct snd_soc_acpi_package_context *ctx) |
Pierre-Louis Bossart | 3421894 | 2016-11-12 18:07:44 -0600 | [diff] [blame] | 100 | { |
| 101 | acpi_status status; |
| 102 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 103 | 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] | 104 | |
| 105 | if (ACPI_FAILURE(status) || !ctx->data_valid) |
| 106 | return false; |
| 107 | |
| 108 | return true; |
| 109 | } |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 110 | EXPORT_SYMBOL_GPL(snd_soc_acpi_find_package_from_hid); |
Pierre-Louis Bossart | 3421894 | 2016-11-12 18:07:44 -0600 | [diff] [blame] | 111 | |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 112 | struct snd_soc_acpi_mach *snd_soc_acpi_codec_list(void *arg) |
Naveen M | 54746da | 2017-05-15 13:42:15 +0530 | [diff] [blame] | 113 | { |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 114 | struct snd_soc_acpi_mach *mach = arg; |
| 115 | struct snd_soc_acpi_codecs *codec_list = |
| 116 | (struct snd_soc_acpi_codecs *) mach->quirk_data; |
Naveen M | 54746da | 2017-05-15 13:42:15 +0530 | [diff] [blame] | 117 | int i; |
| 118 | |
| 119 | if (mach->quirk_data == NULL) |
| 120 | return mach; |
| 121 | |
| 122 | for (i = 0; i < codec_list->num_codecs; i++) { |
Jeremy Cline | 0d5ea12 | 2018-01-05 14:55:34 -0600 | [diff] [blame] | 123 | if (!acpi_dev_present(codec_list->codecs[i], NULL, -1)) |
Naveen M | 54746da | 2017-05-15 13:42:15 +0530 | [diff] [blame] | 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | return mach; |
| 128 | } |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 129 | EXPORT_SYMBOL_GPL(snd_soc_acpi_codec_list); |
Naveen M | 54746da | 2017-05-15 13:42:15 +0530 | [diff] [blame] | 130 | |
Vinod Koul | 8ceffd2 | 2016-02-08 10:45:39 +0530 | [diff] [blame] | 131 | MODULE_LICENSE("GPL v2"); |
Pierre-Louis Bossart | 7feb2f7 | 2017-10-12 18:49:38 -0500 | [diff] [blame] | 132 | MODULE_DESCRIPTION("ALSA SoC ACPI module"); |