• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
intel_nhlt_init(struct device * dev)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 
intel_nhlt_free(struct nhlt_acpi_table * nhlt)56 void intel_nhlt_free(struct nhlt_acpi_table *nhlt)
57 {
58 	memunmap((void *)nhlt);
59 }
60 EXPORT_SYMBOL_GPL(intel_nhlt_free);
61 
intel_nhlt_get_dmic_geo(struct device * dev,struct nhlt_acpi_table * nhlt)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 	struct nhlt_vendor_dmic_array_config *cfg_vendor;
67 	struct nhlt_fmt *fmt_configs;
68 	unsigned int dmic_geo = 0;
69 	u16 max_ch = 0;
70 	u8 i, j;
71 
72 	if (!nhlt)
73 		return 0;
74 
75 	if (nhlt->header.length <= sizeof(struct acpi_table_header)) {
76 		dev_warn(dev, "Invalid DMIC description table\n");
77 		return 0;
78 	}
79 
80 	for (j = 0, epnt = nhlt->desc; j < nhlt->endpoint_count; j++,
81 	     epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length)) {
82 
83 		if (epnt->linktype != NHLT_LINK_DMIC)
84 			continue;
85 
86 		cfg = (struct nhlt_dmic_array_config  *)(epnt->config.caps);
87 		fmt_configs = (struct nhlt_fmt *)(epnt->config.caps + epnt->config.size);
88 
89 		/* find max number of channels based on format_configuration */
90 		if (fmt_configs->fmt_count) {
91 			dev_dbg(dev, "%s: found %d format definitions\n",
92 				__func__, fmt_configs->fmt_count);
93 
94 			for (i = 0; i < fmt_configs->fmt_count; i++) {
95 				struct wav_fmt_ext *fmt_ext;
96 
97 				fmt_ext = &fmt_configs->fmt_config[i].fmt_ext;
98 
99 				if (fmt_ext->fmt.channels > max_ch)
100 					max_ch = fmt_ext->fmt.channels;
101 			}
102 			dev_dbg(dev, "%s: max channels found %d\n", __func__, max_ch);
103 		} else {
104 			dev_dbg(dev, "%s: No format information found\n", __func__);
105 		}
106 
107 		if (cfg->device_config.config_type != NHLT_CONFIG_TYPE_MIC_ARRAY) {
108 			dmic_geo = max_ch;
109 		} else {
110 			switch (cfg->array_type) {
111 			case NHLT_MIC_ARRAY_2CH_SMALL:
112 			case NHLT_MIC_ARRAY_2CH_BIG:
113 				dmic_geo = MIC_ARRAY_2CH;
114 				break;
115 
116 			case NHLT_MIC_ARRAY_4CH_1ST_GEOM:
117 			case NHLT_MIC_ARRAY_4CH_L_SHAPED:
118 			case NHLT_MIC_ARRAY_4CH_2ND_GEOM:
119 				dmic_geo = MIC_ARRAY_4CH;
120 				break;
121 			case NHLT_MIC_ARRAY_VENDOR_DEFINED:
122 				cfg_vendor = (struct nhlt_vendor_dmic_array_config *)cfg;
123 				dmic_geo = cfg_vendor->nb_mics;
124 				break;
125 			default:
126 				dev_warn(dev, "%s: undefined DMIC array_type 0x%0x\n",
127 					 __func__, cfg->array_type);
128 			}
129 
130 			if (dmic_geo > 0) {
131 				dev_dbg(dev, "%s: Array with %d dmics\n", __func__, dmic_geo);
132 			}
133 			if (max_ch > dmic_geo) {
134 				dev_dbg(dev, "%s: max channels %d exceed dmic number %d\n",
135 					__func__, max_ch, dmic_geo);
136 			}
137 		}
138 	}
139 
140 	dev_dbg(dev, "%s: dmic number %d max_ch %d\n",
141 		__func__, dmic_geo, max_ch);
142 
143 	return dmic_geo;
144 }
145 EXPORT_SYMBOL_GPL(intel_nhlt_get_dmic_geo);
146 
147 MODULE_LICENSE("GPL v2");
148 MODULE_DESCRIPTION("Intel NHLT driver");
149