intel-nhlt.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (c) 2015-2019 Intel Corporation
  3. #include <linux/acpi.h>
  4. #include <sound/intel-nhlt.h>
  5. struct nhlt_acpi_table *intel_nhlt_init(struct device *dev)
  6. {
  7. struct nhlt_acpi_table *nhlt;
  8. acpi_status status;
  9. status = acpi_get_table(ACPI_SIG_NHLT, 0,
  10. (struct acpi_table_header **)&nhlt);
  11. if (ACPI_FAILURE(status)) {
  12. dev_warn(dev, "NHLT table not found\n");
  13. return NULL;
  14. }
  15. return nhlt;
  16. }
  17. EXPORT_SYMBOL_GPL(intel_nhlt_init);
  18. void intel_nhlt_free(struct nhlt_acpi_table *nhlt)
  19. {
  20. acpi_put_table((struct acpi_table_header *)nhlt);
  21. }
  22. EXPORT_SYMBOL_GPL(intel_nhlt_free);
  23. int intel_nhlt_get_dmic_geo(struct device *dev, struct nhlt_acpi_table *nhlt)
  24. {
  25. struct nhlt_endpoint *epnt;
  26. struct nhlt_dmic_array_config *cfg;
  27. struct nhlt_vendor_dmic_array_config *cfg_vendor;
  28. struct nhlt_fmt *fmt_configs;
  29. unsigned int dmic_geo = 0;
  30. u16 max_ch = 0;
  31. u8 i, j;
  32. if (!nhlt)
  33. return 0;
  34. if (nhlt->header.length <= sizeof(struct acpi_table_header)) {
  35. dev_warn(dev, "Invalid DMIC description table\n");
  36. return 0;
  37. }
  38. for (j = 0, epnt = nhlt->desc; j < nhlt->endpoint_count; j++,
  39. epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length)) {
  40. if (epnt->linktype != NHLT_LINK_DMIC)
  41. continue;
  42. cfg = (struct nhlt_dmic_array_config *)(epnt->config.caps);
  43. fmt_configs = (struct nhlt_fmt *)(epnt->config.caps + epnt->config.size);
  44. /* find max number of channels based on format_configuration */
  45. if (fmt_configs->fmt_count) {
  46. struct nhlt_fmt_cfg *fmt_cfg = fmt_configs->fmt_config;
  47. dev_dbg(dev, "found %d format definitions\n",
  48. fmt_configs->fmt_count);
  49. for (i = 0; i < fmt_configs->fmt_count; i++) {
  50. struct wav_fmt_ext *fmt_ext;
  51. fmt_ext = &fmt_cfg->fmt_ext;
  52. if (fmt_ext->fmt.channels > max_ch)
  53. max_ch = fmt_ext->fmt.channels;
  54. /* Move to the next nhlt_fmt_cfg */
  55. fmt_cfg = (struct nhlt_fmt_cfg *)(fmt_cfg->config.caps +
  56. fmt_cfg->config.size);
  57. }
  58. dev_dbg(dev, "max channels found %d\n", max_ch);
  59. } else {
  60. dev_dbg(dev, "No format information found\n");
  61. }
  62. if (cfg->device_config.config_type != NHLT_CONFIG_TYPE_MIC_ARRAY) {
  63. dmic_geo = max_ch;
  64. } else {
  65. switch (cfg->array_type) {
  66. case NHLT_MIC_ARRAY_2CH_SMALL:
  67. case NHLT_MIC_ARRAY_2CH_BIG:
  68. dmic_geo = MIC_ARRAY_2CH;
  69. break;
  70. case NHLT_MIC_ARRAY_4CH_1ST_GEOM:
  71. case NHLT_MIC_ARRAY_4CH_L_SHAPED:
  72. case NHLT_MIC_ARRAY_4CH_2ND_GEOM:
  73. dmic_geo = MIC_ARRAY_4CH;
  74. break;
  75. case NHLT_MIC_ARRAY_VENDOR_DEFINED:
  76. cfg_vendor = (struct nhlt_vendor_dmic_array_config *)cfg;
  77. dmic_geo = cfg_vendor->nb_mics;
  78. break;
  79. default:
  80. dev_warn(dev, "%s: undefined DMIC array_type 0x%0x\n",
  81. __func__, cfg->array_type);
  82. }
  83. if (dmic_geo > 0) {
  84. dev_dbg(dev, "Array with %d dmics\n", dmic_geo);
  85. }
  86. if (max_ch > dmic_geo) {
  87. dev_dbg(dev, "max channels %d exceed dmic number %d\n",
  88. max_ch, dmic_geo);
  89. }
  90. }
  91. }
  92. dev_dbg(dev, "dmic number %d max_ch %d\n", dmic_geo, max_ch);
  93. return dmic_geo;
  94. }
  95. EXPORT_SYMBOL_GPL(intel_nhlt_get_dmic_geo);
  96. bool intel_nhlt_has_endpoint_type(struct nhlt_acpi_table *nhlt, u8 link_type)
  97. {
  98. struct nhlt_endpoint *epnt;
  99. int i;
  100. if (!nhlt)
  101. return false;
  102. epnt = (struct nhlt_endpoint *)nhlt->desc;
  103. for (i = 0; i < nhlt->endpoint_count; i++) {
  104. if (epnt->linktype == link_type)
  105. return true;
  106. epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
  107. }
  108. return false;
  109. }
  110. EXPORT_SYMBOL(intel_nhlt_has_endpoint_type);
  111. int intel_nhlt_ssp_endpoint_mask(struct nhlt_acpi_table *nhlt, u8 device_type)
  112. {
  113. struct nhlt_endpoint *epnt;
  114. int ssp_mask = 0;
  115. int i;
  116. if (!nhlt || (device_type != NHLT_DEVICE_BT && device_type != NHLT_DEVICE_I2S))
  117. return 0;
  118. epnt = (struct nhlt_endpoint *)nhlt->desc;
  119. for (i = 0; i < nhlt->endpoint_count; i++) {
  120. if (epnt->linktype == NHLT_LINK_SSP && epnt->device_type == device_type) {
  121. /* for SSP the virtual bus id is the SSP port */
  122. ssp_mask |= BIT(epnt->virtual_bus_id);
  123. }
  124. epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
  125. }
  126. return ssp_mask;
  127. }
  128. EXPORT_SYMBOL(intel_nhlt_ssp_endpoint_mask);
  129. #define SSP_BLOB_V1_0_SIZE 84
  130. #define SSP_BLOB_V1_0_MDIVC_OFFSET 19 /* offset in u32 */
  131. #define SSP_BLOB_V1_5_SIZE 96
  132. #define SSP_BLOB_V1_5_MDIVC_OFFSET 21 /* offset in u32 */
  133. #define SSP_BLOB_VER_1_5 0xEE000105
  134. #define SSP_BLOB_V2_0_SIZE 88
  135. #define SSP_BLOB_V2_0_MDIVC_OFFSET 20 /* offset in u32 */
  136. #define SSP_BLOB_VER_2_0 0xEE000200
  137. int intel_nhlt_ssp_mclk_mask(struct nhlt_acpi_table *nhlt, int ssp_num)
  138. {
  139. struct nhlt_endpoint *epnt;
  140. struct nhlt_fmt *fmt;
  141. struct nhlt_fmt_cfg *cfg;
  142. int mclk_mask = 0;
  143. int i, j;
  144. if (!nhlt)
  145. return 0;
  146. epnt = (struct nhlt_endpoint *)nhlt->desc;
  147. for (i = 0; i < nhlt->endpoint_count; i++) {
  148. /* we only care about endpoints connected to an audio codec over SSP */
  149. if (epnt->linktype == NHLT_LINK_SSP &&
  150. epnt->device_type == NHLT_DEVICE_I2S &&
  151. epnt->virtual_bus_id == ssp_num) {
  152. fmt = (struct nhlt_fmt *)(epnt->config.caps + epnt->config.size);
  153. cfg = fmt->fmt_config;
  154. /*
  155. * In theory all formats should use the same MCLK but it doesn't hurt to
  156. * double-check that the configuration is consistent
  157. */
  158. for (j = 0; j < fmt->fmt_count; j++) {
  159. u32 *blob;
  160. int mdivc_offset;
  161. int size;
  162. /* first check we have enough data to read the blob type */
  163. if (cfg->config.size < 8)
  164. return -EINVAL;
  165. blob = (u32 *)cfg->config.caps;
  166. if (blob[1] == SSP_BLOB_VER_2_0) {
  167. mdivc_offset = SSP_BLOB_V2_0_MDIVC_OFFSET;
  168. size = SSP_BLOB_V2_0_SIZE;
  169. } else if (blob[1] == SSP_BLOB_VER_1_5) {
  170. mdivc_offset = SSP_BLOB_V1_5_MDIVC_OFFSET;
  171. size = SSP_BLOB_V1_5_SIZE;
  172. } else {
  173. mdivc_offset = SSP_BLOB_V1_0_MDIVC_OFFSET;
  174. size = SSP_BLOB_V1_0_SIZE;
  175. }
  176. /* make sure we have enough data for the fixed part of the blob */
  177. if (cfg->config.size < size)
  178. return -EINVAL;
  179. mclk_mask |= blob[mdivc_offset] & GENMASK(1, 0);
  180. cfg = (struct nhlt_fmt_cfg *)(cfg->config.caps + cfg->config.size);
  181. }
  182. }
  183. epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
  184. }
  185. /* make sure only one MCLK is used */
  186. if (hweight_long(mclk_mask) != 1)
  187. return -EINVAL;
  188. return mclk_mask;
  189. }
  190. EXPORT_SYMBOL(intel_nhlt_ssp_mclk_mask);
  191. static struct nhlt_specific_cfg *
  192. nhlt_get_specific_cfg(struct device *dev, struct nhlt_fmt *fmt, u8 num_ch,
  193. u32 rate, u8 vbps, u8 bps)
  194. {
  195. struct nhlt_fmt_cfg *cfg = fmt->fmt_config;
  196. struct wav_fmt *wfmt;
  197. u16 _bps, _vbps;
  198. int i;
  199. dev_dbg(dev, "Endpoint format count=%d\n", fmt->fmt_count);
  200. for (i = 0; i < fmt->fmt_count; i++) {
  201. wfmt = &cfg->fmt_ext.fmt;
  202. _bps = wfmt->bits_per_sample;
  203. _vbps = cfg->fmt_ext.sample.valid_bits_per_sample;
  204. dev_dbg(dev, "Endpoint format: ch=%d fmt=%d/%d rate=%d\n",
  205. wfmt->channels, _vbps, _bps, wfmt->samples_per_sec);
  206. if (wfmt->channels == num_ch && wfmt->samples_per_sec == rate &&
  207. vbps == _vbps && bps == _bps)
  208. return &cfg->config;
  209. cfg = (struct nhlt_fmt_cfg *)(cfg->config.caps + cfg->config.size);
  210. }
  211. return NULL;
  212. }
  213. static bool nhlt_check_ep_match(struct device *dev, struct nhlt_endpoint *epnt,
  214. u32 bus_id, u8 link_type, u8 dir, u8 dev_type)
  215. {
  216. dev_dbg(dev, "Endpoint: vbus_id=%d link_type=%d dir=%d dev_type = %d\n",
  217. epnt->virtual_bus_id, epnt->linktype,
  218. epnt->direction, epnt->device_type);
  219. if ((epnt->virtual_bus_id != bus_id) ||
  220. (epnt->linktype != link_type) ||
  221. (epnt->direction != dir))
  222. return false;
  223. /* link of type DMIC bypasses device_type check */
  224. return epnt->linktype == NHLT_LINK_DMIC ||
  225. epnt->device_type == dev_type;
  226. }
  227. struct nhlt_specific_cfg *
  228. intel_nhlt_get_endpoint_blob(struct device *dev, struct nhlt_acpi_table *nhlt,
  229. u32 bus_id, u8 link_type, u8 vbps, u8 bps,
  230. u8 num_ch, u32 rate, u8 dir, u8 dev_type)
  231. {
  232. struct nhlt_specific_cfg *cfg;
  233. struct nhlt_endpoint *epnt;
  234. struct nhlt_fmt *fmt;
  235. int i;
  236. if (!nhlt)
  237. return NULL;
  238. dev_dbg(dev, "Looking for configuration:\n");
  239. dev_dbg(dev, " vbus_id=%d link_type=%d dir=%d, dev_type=%d\n",
  240. bus_id, link_type, dir, dev_type);
  241. dev_dbg(dev, " ch=%d fmt=%d/%d rate=%d\n", num_ch, vbps, bps, rate);
  242. dev_dbg(dev, "Endpoint count=%d\n", nhlt->endpoint_count);
  243. epnt = (struct nhlt_endpoint *)nhlt->desc;
  244. for (i = 0; i < nhlt->endpoint_count; i++) {
  245. if (nhlt_check_ep_match(dev, epnt, bus_id, link_type, dir, dev_type)) {
  246. fmt = (struct nhlt_fmt *)(epnt->config.caps + epnt->config.size);
  247. cfg = nhlt_get_specific_cfg(dev, fmt, num_ch, rate, vbps, bps);
  248. if (cfg)
  249. return cfg;
  250. }
  251. epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
  252. }
  253. return NULL;
  254. }
  255. EXPORT_SYMBOL(intel_nhlt_get_endpoint_blob);