ALSA: hda: move parts of NHLT code to new module
[linux-2.6-block.git] / sound / hda / intel-nhlt.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2015-2019 Intel Corporation
3
4 #include <linux/acpi.h>
5 #include <sound/intel-nhlt.h>
6
7 #define NHLT_ACPI_HEADER_SIG    "NHLT"
8
9 /* Unique identification for getting NHLT blobs */
10 static guid_t osc_guid =
11         GUID_INIT(0xA69F886E, 0x6CEB, 0x4594,
12                   0xA4, 0x1F, 0x7B, 0x5D, 0xCE, 0x24, 0xC5, 0x53);
13
14 struct nhlt_acpi_table *intel_nhlt_init(struct device *dev)
15 {
16         acpi_handle handle;
17         union acpi_object *obj;
18         struct nhlt_resource_desc *nhlt_ptr;
19         struct nhlt_acpi_table *nhlt_table = NULL;
20
21         handle = ACPI_HANDLE(dev);
22         if (!handle) {
23                 dev_err(dev, "Didn't find ACPI_HANDLE\n");
24                 return NULL;
25         }
26
27         obj = acpi_evaluate_dsm(handle, &osc_guid, 1, 1, NULL);
28
29         if (!obj)
30                 return NULL;
31
32         if (obj->type != ACPI_TYPE_BUFFER) {
33                 dev_dbg(dev, "No NHLT table found\n");
34                 ACPI_FREE(obj);
35                 return NULL;
36         }
37
38         nhlt_ptr = (struct nhlt_resource_desc  *)obj->buffer.pointer;
39         if (nhlt_ptr->length)
40                 nhlt_table = (struct nhlt_acpi_table *)
41                         memremap(nhlt_ptr->min_addr, nhlt_ptr->length,
42                                  MEMREMAP_WB);
43         ACPI_FREE(obj);
44         if (nhlt_table &&
45             (strncmp(nhlt_table->header.signature,
46                      NHLT_ACPI_HEADER_SIG,
47                      strlen(NHLT_ACPI_HEADER_SIG)) != 0)) {
48                 memunmap(nhlt_table);
49                 dev_err(dev, "NHLT ACPI header signature incorrect\n");
50                 return NULL;
51         }
52         return nhlt_table;
53 }
54 EXPORT_SYMBOL_GPL(intel_nhlt_init);
55
56 void intel_nhlt_free(struct nhlt_acpi_table *nhlt)
57 {
58         memunmap((void *)nhlt);
59 }
60 EXPORT_SYMBOL_GPL(intel_nhlt_free);
61
62 int intel_nhlt_get_dmic_geo(struct device *dev, struct nhlt_acpi_table *nhlt)
63 {
64         struct nhlt_endpoint *epnt;
65         struct nhlt_dmic_array_config *cfg;
66         unsigned int dmic_geo = 0;
67         u8 j;
68
69         if (!nhlt)
70                 return 0;
71
72         epnt = (struct nhlt_endpoint *)nhlt->desc;
73
74         for (j = 0; j < nhlt->endpoint_count; j++) {
75                 if (epnt->linktype == NHLT_LINK_DMIC) {
76                         cfg = (struct nhlt_dmic_array_config  *)
77                                         (epnt->config.caps);
78                         switch (cfg->array_type) {
79                         case NHLT_MIC_ARRAY_2CH_SMALL:
80                         case NHLT_MIC_ARRAY_2CH_BIG:
81                                 dmic_geo = MIC_ARRAY_2CH;
82                                 break;
83
84                         case NHLT_MIC_ARRAY_4CH_1ST_GEOM:
85                         case NHLT_MIC_ARRAY_4CH_L_SHAPED:
86                         case NHLT_MIC_ARRAY_4CH_2ND_GEOM:
87                                 dmic_geo = MIC_ARRAY_4CH;
88                                 break;
89
90                         default:
91                                 dev_warn(dev, "undefined DMIC array_type 0x%0x\n",
92                                          cfg->array_type);
93                         }
94                 }
95                 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
96         }
97
98         return dmic_geo;
99 }
100 EXPORT_SYMBOL_GPL(intel_nhlt_get_dmic_geo);
101
102 MODULE_LICENSE("GPL v2");
103 MODULE_DESCRIPTION("Intel NHLT driver");