1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright (c) 2015-2019 Intel Corporation 3 4 #include <linux/acpi.h> 5 #include <sound/intel-nhlt.h> 6 7 struct nhlt_acpi_table *intel_nhlt_init(struct device *dev) 8 { 9 struct nhlt_acpi_table *nhlt; 10 acpi_status status; 11 12 status = acpi_get_table(ACPI_SIG_NHLT, 0, 13 (struct acpi_table_header **)&nhlt); 14 if (ACPI_FAILURE(status)) { 15 dev_warn(dev, "NHLT table not found\n"); 16 return NULL; 17 } 18 19 return nhlt; 20 } 21 EXPORT_SYMBOL_GPL(intel_nhlt_init); 22 23 void intel_nhlt_free(struct nhlt_acpi_table *nhlt) 24 { 25 acpi_put_table((struct acpi_table_header *)nhlt); 26 } 27 EXPORT_SYMBOL_GPL(intel_nhlt_free); 28 29 int intel_nhlt_get_dmic_geo(struct device *dev, struct nhlt_acpi_table *nhlt) 30 { 31 struct nhlt_endpoint *epnt; 32 struct nhlt_dmic_array_config *cfg; 33 struct nhlt_vendor_dmic_array_config *cfg_vendor; 34 struct nhlt_fmt *fmt_configs; 35 unsigned int dmic_geo = 0; 36 u16 max_ch = 0; 37 u8 i, j; 38 39 if (!nhlt) 40 return 0; 41 42 if (nhlt->header.length <= sizeof(struct acpi_table_header)) { 43 dev_warn(dev, "Invalid DMIC description table\n"); 44 return 0; 45 } 46 47 for (j = 0, epnt = nhlt->desc; j < nhlt->endpoint_count; j++, 48 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length)) { 49 50 if (epnt->linktype != NHLT_LINK_DMIC) 51 continue; 52 53 cfg = (struct nhlt_dmic_array_config *)(epnt->config.caps); 54 fmt_configs = (struct nhlt_fmt *)(epnt->config.caps + epnt->config.size); 55 56 /* find max number of channels based on format_configuration */ 57 if (fmt_configs->fmt_count) { 58 dev_dbg(dev, "%s: found %d format definitions\n", 59 __func__, fmt_configs->fmt_count); 60 61 for (i = 0; i < fmt_configs->fmt_count; i++) { 62 struct wav_fmt_ext *fmt_ext; 63 64 fmt_ext = &fmt_configs->fmt_config[i].fmt_ext; 65 66 if (fmt_ext->fmt.channels > max_ch) 67 max_ch = fmt_ext->fmt.channels; 68 } 69 dev_dbg(dev, "%s: max channels found %d\n", __func__, max_ch); 70 } else { 71 dev_dbg(dev, "%s: No format information found\n", __func__); 72 } 73 74 if (cfg->device_config.config_type != NHLT_CONFIG_TYPE_MIC_ARRAY) { 75 dmic_geo = max_ch; 76 } else { 77 switch (cfg->array_type) { 78 case NHLT_MIC_ARRAY_2CH_SMALL: 79 case NHLT_MIC_ARRAY_2CH_BIG: 80 dmic_geo = MIC_ARRAY_2CH; 81 break; 82 83 case NHLT_MIC_ARRAY_4CH_1ST_GEOM: 84 case NHLT_MIC_ARRAY_4CH_L_SHAPED: 85 case NHLT_MIC_ARRAY_4CH_2ND_GEOM: 86 dmic_geo = MIC_ARRAY_4CH; 87 break; 88 case NHLT_MIC_ARRAY_VENDOR_DEFINED: 89 cfg_vendor = (struct nhlt_vendor_dmic_array_config *)cfg; 90 dmic_geo = cfg_vendor->nb_mics; 91 break; 92 default: 93 dev_warn(dev, "%s: undefined DMIC array_type 0x%0x\n", 94 __func__, cfg->array_type); 95 } 96 97 if (dmic_geo > 0) { 98 dev_dbg(dev, "%s: Array with %d dmics\n", __func__, dmic_geo); 99 } 100 if (max_ch > dmic_geo) { 101 dev_dbg(dev, "%s: max channels %d exceed dmic number %d\n", 102 __func__, max_ch, dmic_geo); 103 } 104 } 105 } 106 107 dev_dbg(dev, "%s: dmic number %d max_ch %d\n", 108 __func__, dmic_geo, max_ch); 109 110 return dmic_geo; 111 } 112 EXPORT_SYMBOL_GPL(intel_nhlt_get_dmic_geo); 113 114 bool intel_nhlt_has_endpoint_type(struct nhlt_acpi_table *nhlt, u8 link_type) 115 { 116 struct nhlt_endpoint *epnt; 117 int i; 118 119 if (!nhlt) 120 return false; 121 122 epnt = (struct nhlt_endpoint *)nhlt->desc; 123 for (i = 0; i < nhlt->endpoint_count; i++) { 124 if (epnt->linktype == link_type) 125 return true; 126 127 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length); 128 } 129 return false; 130 } 131 EXPORT_SYMBOL(intel_nhlt_has_endpoint_type); 132 133 static struct nhlt_specific_cfg * 134 nhlt_get_specific_cfg(struct device *dev, struct nhlt_fmt *fmt, u8 num_ch, 135 u32 rate, u8 vbps, u8 bps) 136 { 137 struct nhlt_fmt_cfg *cfg = fmt->fmt_config; 138 struct wav_fmt *wfmt; 139 u16 _bps, _vbps; 140 int i; 141 142 dev_dbg(dev, "Endpoint format count=%d\n", fmt->fmt_count); 143 144 for (i = 0; i < fmt->fmt_count; i++) { 145 wfmt = &cfg->fmt_ext.fmt; 146 _bps = wfmt->bits_per_sample; 147 _vbps = cfg->fmt_ext.sample.valid_bits_per_sample; 148 149 dev_dbg(dev, "Endpoint format: ch=%d fmt=%d/%d rate=%d\n", 150 wfmt->channels, _vbps, _bps, wfmt->samples_per_sec); 151 152 if (wfmt->channels == num_ch && wfmt->samples_per_sec == rate && 153 vbps == _vbps && bps == _bps) 154 return &cfg->config; 155 156 cfg = (struct nhlt_fmt_cfg *)(cfg->config.caps + cfg->config.size); 157 } 158 159 return NULL; 160 } 161 162 static bool nhlt_check_ep_match(struct device *dev, struct nhlt_endpoint *epnt, 163 u32 bus_id, u8 link_type, u8 dir, u8 dev_type) 164 { 165 dev_dbg(dev, "Endpoint: vbus_id=%d link_type=%d dir=%d dev_type = %d\n", 166 epnt->virtual_bus_id, epnt->linktype, 167 epnt->direction, epnt->device_type); 168 169 if ((epnt->virtual_bus_id != bus_id) || 170 (epnt->linktype != link_type) || 171 (epnt->direction != dir)) 172 return false; 173 174 /* link of type DMIC bypasses device_type check */ 175 return epnt->linktype == NHLT_LINK_DMIC || 176 epnt->device_type == dev_type; 177 } 178 179 struct nhlt_specific_cfg * 180 intel_nhlt_get_endpoint_blob(struct device *dev, struct nhlt_acpi_table *nhlt, 181 u32 bus_id, u8 link_type, u8 vbps, u8 bps, 182 u8 num_ch, u32 rate, u8 dir, u8 dev_type) 183 { 184 struct nhlt_specific_cfg *cfg; 185 struct nhlt_endpoint *epnt; 186 struct nhlt_fmt *fmt; 187 int i; 188 189 if (!nhlt) 190 return NULL; 191 192 dev_dbg(dev, "Looking for configuration:\n"); 193 dev_dbg(dev, " vbus_id=%d link_type=%d dir=%d, dev_type=%d\n", 194 bus_id, link_type, dir, dev_type); 195 dev_dbg(dev, " ch=%d fmt=%d/%d rate=%d\n", num_ch, vbps, bps, rate); 196 dev_dbg(dev, "Endpoint count=%d\n", nhlt->endpoint_count); 197 198 epnt = (struct nhlt_endpoint *)nhlt->desc; 199 200 for (i = 0; i < nhlt->endpoint_count; i++) { 201 if (nhlt_check_ep_match(dev, epnt, bus_id, link_type, dir, dev_type)) { 202 fmt = (struct nhlt_fmt *)(epnt->config.caps + epnt->config.size); 203 204 cfg = nhlt_get_specific_cfg(dev, fmt, num_ch, rate, vbps, bps); 205 if (cfg) 206 return cfg; 207 } 208 209 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length); 210 } 211 212 return NULL; 213 } 214 EXPORT_SYMBOL(intel_nhlt_get_endpoint_blob); 215