1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright(c) 2023-2024 Intel Corporation
*
* Authors: Cezary Rojewski <cezary.rojewski@intel.com>
* Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
*/
#define pr_fmt(fmt) "ACPI: NHLT: " fmt
#include <linux/acpi.h>
#include <linux/errno.h>
#include <linux/export.h>
#include <linux/minmax.h>
#include <linux/printk.h>
#include <linux/types.h>
#include <acpi/nhlt.h>
static struct acpi_table_nhlt *acpi_gbl_nhlt;
static struct acpi_table_nhlt empty_nhlt = {
.header = {
.signature = ACPI_SIG_NHLT,
},
};
/**
* acpi_nhlt_get_gbl_table - Retrieve a pointer to the first NHLT table.
*
* If there is no NHLT in the system, acpi_gbl_nhlt will instead point to an
* empty table.
*
* Return: ACPI status code of the operation.
*/
acpi_status acpi_nhlt_get_gbl_table(void)
{
acpi_status status;
status = acpi_get_table(ACPI_SIG_NHLT, 0, (struct acpi_table_header **)(&acpi_gbl_nhlt));
if (!acpi_gbl_nhlt)
acpi_gbl_nhlt = &empty_nhlt;
return status;
}
EXPORT_SYMBOL_GPL(acpi_nhlt_get_gbl_table);
/**
* acpi_nhlt_put_gbl_table - Release the global NHLT table.
*/
void acpi_nhlt_put_gbl_table(void)
{
acpi_put_table((struct acpi_table_header *)acpi_gbl_nhlt);
}
EXPORT_SYMBOL_GPL(acpi_nhlt_put_gbl_table);
/**
* acpi_nhlt_endpoint_match - Verify if an endpoint matches criteria.
* @ep: the endpoint to check.
* @link_type: the hardware link type, e.g.: PDM or SSP.
* @dev_type: the device type.
* @dir: stream direction.
* @bus_id: the ID of virtual bus hosting the endpoint.
*
* Either of @link_type, @dev_type, @dir or @bus_id may be set to a negative
* value to ignore the parameter when matching.
*
* Return: %true if endpoint matches specified criteria or %false otherwise.
*/
bool acpi_nhlt_endpoint_match(const struct acpi_nhlt_endpoint *ep,
int link_type, int dev_type, int dir, int bus_id)
{
return ep &&
(link_type < 0 || ep->link_type == link_type) &&
(dev_type < 0 || ep->device_type == dev_type) &&
(bus_id < 0 || ep->virtual_bus_id == bus_id) &&
(dir < 0 || ep->direction == dir);
}
EXPORT_SYMBOL_GPL(acpi_nhlt_endpoint_match);
/**
* acpi_nhlt_tb_find_endpoint - Search a NHLT table for an endpoint.
* @tb: the table to search.
* @link_type: the hardware link type, e.g.: PDM or SSP.
* @dev_type: the device type.
* @dir: stream direction.
* @bus_id: the ID of virtual bus hosting the endpoint.
*
* Either of @link_type, @dev_type, @dir or @bus_id may be set to a negative
* value to ignore the parameter during the search.
*
* Return: A pointer to endpoint matching the criteria, %NULL if not found or
* an ERR_PTR() otherwise.
*/
struct acpi_nhlt_endpoint *
acpi_nhlt_tb_find_endpoint(const struct acpi_table_nhlt *tb,
int link_type, int dev_type, int dir, int bus_id)
{
struct acpi_nhlt_endpoint *ep;
for_each_nhlt_endpoint(tb, ep)
if (acpi_nhlt_endpoint_match(ep, link_type, dev_type, dir, bus_id))
return ep;
return NULL;
}
EXPORT_SYMBOL_GPL(acpi_nhlt_tb_find_endpoint);
/**
* acpi_nhlt_find_endpoint - Search all NHLT tables for an endpoint.
* @link_type: the hardware link type, e.g.: PDM or SSP.
* @dev_type: the device type.
* @dir: stream direction.
* @bus_id: the ID of virtual bus hosting the endpoint.
*
* Either of @link_type, @dev_type, @dir or @bus_id may be set to a negative
* value to ignore the parameter during the search.
*
* Return: A pointer to endpoint matching the criteria, %NULL if not found or
* an ERR_PTR() otherwise.
*/
struct acpi_nhlt_endpoint *
acpi_nhlt_find_endpoint(int link_type, int dev_type, int dir, int bus_id)
{
/* TODO: Currently limited to table of index 0. */
return acpi_nhlt_tb_find_endpoint(acpi_gbl_nhlt, link_type, dev_type, dir, bus_id);
}
EXPORT_SYMBOL_GPL(acpi_nhlt_find_endpoint);
/**
* acpi_nhlt_endpoint_find_fmtcfg - Search endpoint's formats configuration space
* for a specific format.
* @ep: the endpoint to search.
* @ch: number of channels.
* @rate: samples per second.
* @vbps: valid bits per sample.
* @bps: bits per sample.
*
* Return: A pointer to format matching the criteria, %NULL if not found or
* an ERR_PTR() otherwise.
*/
struct acpi_nhlt_format_config *
acpi_nhlt_endpoint_find_fmtcfg(const struct acpi_nhlt_endpoint *ep,
u16 ch, u32 rate, u16 vbps, u16 bps)
{
struct acpi_nhlt_wave_formatext *wav;
struct acpi_nhlt_format_config *fmt;
for_each_nhlt_endpoint_fmtcfg(ep, fmt) {
wav = &fmt->format;
if (wav->valid_bits_per_sample == vbps &&
wav->samples_per_sec == rate &&
wav->bits_per_sample == bps &&
wav->channel_count == ch)
return fmt;
}
return NULL;
}
EXPORT_SYMBOL_GPL(acpi_nhlt_endpoint_find_fmtcfg);
/**
* acpi_nhlt_tb_find_fmtcfg - Search a NHLT table for a specific format.
* @tb: the table to search.
* @link_type: the hardware link type, e.g.: PDM or SSP.
* @dev_type: the device type.
* @dir: stream direction.
* @bus_id: the ID of virtual bus hosting the endpoint.
*
* @ch: number of channels.
* @rate: samples per second.
* @vbps: valid bits per sample.
* @bps: bits per sample.
*
* Either of @link_type, @dev_type, @dir or @bus_id may be set to a negative
* value to ignore the parameter during the search.
*
* Return: A pointer to format matching the criteria, %NULL if not found or
* an ERR_PTR() otherwise.
*/
struct acpi_nhlt_format_config *
acpi_nhlt_tb_find_fmtcfg(const struct acpi_table_nhlt *tb,
int link_type, int dev_type, int dir, int bus_id,
u16 ch, u32 rate, u16 vbps, u16 bps)
{
struct acpi_nhlt_format_config *fmt;
struct acpi_nhlt_endpoint *ep;
for_each_nhlt_endpoint(tb, ep) {
if (!acpi_nhlt_endpoint_match(ep, link_type, dev_type, dir, bus_id))
continue;
fmt = acpi_nhlt_endpoint_find_fmtcfg(ep, ch, rate, vbps, bps);
if (fmt)
return fmt;
}
return NULL;
}
EXPORT_SYMBOL_GPL(acpi_nhlt_tb_find_fmtcfg);
/**
* acpi_nhlt_find_fmtcfg - Search all NHLT tables for a specific format.
* @link_type: the hardware link type, e.g.: PDM or SSP.
* @dev_type: the device type.
* @dir: stream direction.
* @bus_id: the ID of virtual bus hosting the endpoint.
*
* @ch: number of channels.
* @rate: samples per second.
* @vbps: valid bits per sample.
* @bps: bits per sample.
*
* Either of @link_type, @dev_type, @dir or @bus_id may be set to a negative
* value to ignore the parameter during the search.
*
* Return: A pointer to format matching the criteria, %NULL if not found or
* an ERR_PTR() otherwise.
*/
struct acpi_nhlt_format_config *
acpi_nhlt_find_fmtcfg(int link_type, int dev_type, int dir, int bus_id,
u16 ch, u32 rate, u16 vbps, u16 bps)
{
/* TODO: Currently limited to table of index 0. */
return acpi_nhlt_tb_find_fmtcfg(acpi_gbl_nhlt, link_type, dev_type, dir, bus_id,
ch, rate, vbps, bps);
}
EXPORT_SYMBOL_GPL(acpi_nhlt_find_fmtcfg);
static bool acpi_nhlt_config_is_micdevice(struct acpi_nhlt_config *cfg)
{
return cfg->capabilities_size >= sizeof(struct acpi_nhlt_micdevice_config);
}
static bool acpi_nhlt_config_is_vendor_micdevice(struct acpi_nhlt_config *cfg)
{
struct acpi_nhlt_vendor_micdevice_config *devcfg = __acpi_nhlt_config_caps(cfg);
return cfg->capabilities_size >= sizeof(*devcfg) &&
cfg->capabilities_size == struct_size(devcfg, mics, devcfg->mics_count);
}
/**
* acpi_nhlt_endpoint_mic_count - Retrieve number of digital microphones for a PDM endpoint.
* @ep: the endpoint to return microphones count for.
*
* Return: A number of microphones or an error code if an invalid endpoint is provided.
*/
int acpi_nhlt_endpoint_mic_count(const struct acpi_nhlt_endpoint *ep)
{
union acpi_nhlt_device_config *devcfg;
struct acpi_nhlt_format_config *fmt;
struct acpi_nhlt_config *cfg;
u16 max_ch = 0;
if (!ep || ep->link_type != ACPI_NHLT_LINKTYPE_PDM)
return -EINVAL;
/* Find max number of channels based on formats configuration. */
for_each_nhlt_endpoint_fmtcfg(ep, fmt)
max_ch = max(fmt->format.channel_count, max_ch);
cfg = __acpi_nhlt_endpoint_config(ep);
devcfg = __acpi_nhlt_config_caps(cfg);
/* If @ep is not a mic array, fallback to channels count. */
if (!acpi_nhlt_config_is_micdevice(cfg) ||
devcfg->gen.config_type != ACPI_NHLT_CONFIGTYPE_MICARRAY)
return max_ch;
switch (devcfg->mic.array_type) {
case ACPI_NHLT_ARRAYTYPE_LINEAR2_SMALL:
case ACPI_NHLT_ARRAYTYPE_LINEAR2_BIG:
return 2;
case ACPI_NHLT_ARRAYTYPE_LINEAR4_GEO1:
case ACPI_NHLT_ARRAYTYPE_PLANAR4_LSHAPED:
case ACPI_NHLT_ARRAYTYPE_LINEAR4_GEO2:
return 4;
case ACPI_NHLT_ARRAYTYPE_VENDOR:
if (!acpi_nhlt_config_is_vendor_micdevice(cfg))
return -EINVAL;
return devcfg->vendor_mic.mics_count;
default:
pr_warn("undefined mic array type: %#x\n", devcfg->mic.array_type);
return max_ch;
}
}
EXPORT_SYMBOL_GPL(acpi_nhlt_endpoint_mic_count);
|