nhlt.c 8.7 KB

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