skl-nhlt.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * skl-nhlt.c - Intel SKL Platform NHLT parsing
  3. *
  4. * Copyright (C) 2015 Intel Corp
  5. * Author: Sanjiv Kumar <sanjiv.kumar@intel.com>
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. *
  19. */
  20. #include <linux/pci.h>
  21. #include "skl.h"
  22. /* Unique identification for getting NHLT blobs */
  23. static u8 OSC_UUID[16] = {0x6E, 0x88, 0x9F, 0xA6, 0xEB, 0x6C, 0x94, 0x45,
  24. 0xA4, 0x1F, 0x7B, 0x5D, 0xCE, 0x24, 0xC5, 0x53};
  25. #define DSDT_NHLT_PATH "\\_SB.PCI0.HDAS"
  26. struct nhlt_acpi_table *skl_nhlt_init(struct device *dev)
  27. {
  28. acpi_handle handle;
  29. union acpi_object *obj;
  30. struct nhlt_resource_desc *nhlt_ptr = NULL;
  31. struct nhlt_acpi_table *nhlt_table = NULL;
  32. if (ACPI_FAILURE(acpi_get_handle(NULL, DSDT_NHLT_PATH, &handle))) {
  33. dev_err(dev, "Requested NHLT device not found\n");
  34. return NULL;
  35. }
  36. obj = acpi_evaluate_dsm(handle, OSC_UUID, 1, 1, NULL);
  37. if (obj && obj->type == ACPI_TYPE_BUFFER) {
  38. nhlt_ptr = (struct nhlt_resource_desc *)obj->buffer.pointer;
  39. nhlt_table = (struct nhlt_acpi_table *)
  40. memremap(nhlt_ptr->min_addr, nhlt_ptr->length,
  41. MEMREMAP_WB);
  42. ACPI_FREE(obj);
  43. return nhlt_table;
  44. }
  45. dev_err(dev, "device specific method to extract NHLT blob failed\n");
  46. return NULL;
  47. }
  48. void skl_nhlt_free(struct nhlt_acpi_table *nhlt)
  49. {
  50. memunmap((void *) nhlt);
  51. }
  52. static struct nhlt_specific_cfg *skl_get_specific_cfg(
  53. struct device *dev, struct nhlt_fmt *fmt,
  54. u8 no_ch, u32 rate, u16 bps, u8 linktype)
  55. {
  56. struct nhlt_specific_cfg *sp_config;
  57. struct wav_fmt *wfmt;
  58. struct nhlt_fmt_cfg *fmt_config = fmt->fmt_config;
  59. int i;
  60. dev_dbg(dev, "Format count =%d\n", fmt->fmt_count);
  61. for (i = 0; i < fmt->fmt_count; i++) {
  62. wfmt = &fmt_config->fmt_ext.fmt;
  63. dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", wfmt->channels,
  64. wfmt->bits_per_sample, wfmt->samples_per_sec);
  65. if (wfmt->channels == no_ch && wfmt->bits_per_sample == bps) {
  66. /*
  67. * if link type is dmic ignore rate check as the blob is
  68. * generic for all rates
  69. */
  70. sp_config = &fmt_config->config;
  71. if (linktype == NHLT_LINK_DMIC)
  72. return sp_config;
  73. if (wfmt->samples_per_sec == rate)
  74. return sp_config;
  75. }
  76. fmt_config = (struct nhlt_fmt_cfg *)(fmt_config->config.caps +
  77. fmt_config->config.size);
  78. }
  79. return NULL;
  80. }
  81. static void dump_config(struct device *dev, u32 instance_id, u8 linktype,
  82. u8 s_fmt, u8 num_channels, u32 s_rate, u8 dirn, u16 bps)
  83. {
  84. dev_dbg(dev, "Input configuration\n");
  85. dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", num_channels, s_fmt, s_rate);
  86. dev_dbg(dev, "vbus_id=%d link_type=%d\n", instance_id, linktype);
  87. dev_dbg(dev, "bits_per_sample=%d\n", bps);
  88. }
  89. static bool skl_check_ep_match(struct device *dev, struct nhlt_endpoint *epnt,
  90. u32 instance_id, u8 link_type, u8 dirn)
  91. {
  92. dev_dbg(dev, "vbus_id=%d link_type=%d dir=%d\n",
  93. epnt->virtual_bus_id, epnt->linktype, epnt->direction);
  94. if ((epnt->virtual_bus_id == instance_id) &&
  95. (epnt->linktype == link_type) &&
  96. (epnt->direction == dirn))
  97. return true;
  98. else
  99. return false;
  100. }
  101. struct nhlt_specific_cfg
  102. *skl_get_ep_blob(struct skl *skl, u32 instance, u8 link_type,
  103. u8 s_fmt, u8 num_ch, u32 s_rate, u8 dirn)
  104. {
  105. struct nhlt_fmt *fmt;
  106. struct nhlt_endpoint *epnt;
  107. struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
  108. struct device *dev = bus->dev;
  109. struct nhlt_specific_cfg *sp_config;
  110. struct nhlt_acpi_table *nhlt = skl->nhlt;
  111. u16 bps = (s_fmt == 16) ? 16 : 32;
  112. u8 j;
  113. dump_config(dev, instance, link_type, s_fmt, num_ch, s_rate, dirn, bps);
  114. epnt = (struct nhlt_endpoint *)nhlt->desc;
  115. dev_dbg(dev, "endpoint count =%d\n", nhlt->endpoint_count);
  116. for (j = 0; j < nhlt->endpoint_count; j++) {
  117. if (skl_check_ep_match(dev, epnt, instance, link_type, dirn)) {
  118. fmt = (struct nhlt_fmt *)(epnt->config.caps +
  119. epnt->config.size);
  120. sp_config = skl_get_specific_cfg(dev, fmt, num_ch,
  121. s_rate, bps, link_type);
  122. if (sp_config)
  123. return sp_config;
  124. }
  125. epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
  126. }
  127. return NULL;
  128. }
  129. int skl_get_dmic_geo(struct skl *skl)
  130. {
  131. struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
  132. struct nhlt_endpoint *epnt;
  133. struct nhlt_dmic_array_config *cfg;
  134. struct device *dev = &skl->pci->dev;
  135. unsigned int dmic_geo = 0;
  136. u8 j;
  137. epnt = (struct nhlt_endpoint *)nhlt->desc;
  138. for (j = 0; j < nhlt->endpoint_count; j++) {
  139. if (epnt->linktype == NHLT_LINK_DMIC) {
  140. cfg = (struct nhlt_dmic_array_config *)
  141. (epnt->config.caps);
  142. switch (cfg->array_type) {
  143. case NHLT_MIC_ARRAY_2CH_SMALL:
  144. case NHLT_MIC_ARRAY_2CH_BIG:
  145. dmic_geo |= MIC_ARRAY_2CH;
  146. break;
  147. case NHLT_MIC_ARRAY_4CH_1ST_GEOM:
  148. case NHLT_MIC_ARRAY_4CH_L_SHAPED:
  149. case NHLT_MIC_ARRAY_4CH_2ND_GEOM:
  150. dmic_geo |= MIC_ARRAY_4CH;
  151. break;
  152. default:
  153. dev_warn(dev, "undefined DMIC array_type 0x%0x\n",
  154. cfg->array_type);
  155. }
  156. }
  157. epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
  158. }
  159. return dmic_geo;
  160. }
  161. static void skl_nhlt_trim_space(struct skl *skl)
  162. {
  163. char *s = skl->tplg_name;
  164. int cnt;
  165. int i;
  166. cnt = 0;
  167. for (i = 0; s[i]; i++) {
  168. if (!isspace(s[i]))
  169. s[cnt++] = s[i];
  170. }
  171. s[cnt] = '\0';
  172. }
  173. int skl_nhlt_update_topology_bin(struct skl *skl)
  174. {
  175. struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
  176. struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
  177. struct device *dev = bus->dev;
  178. dev_dbg(dev, "oem_id %.6s, oem_table_id %8s oem_revision %d\n",
  179. nhlt->header.oem_id, nhlt->header.oem_table_id,
  180. nhlt->header.oem_revision);
  181. snprintf(skl->tplg_name, sizeof(skl->tplg_name), "%x-%.6s-%.8s-%d%s",
  182. skl->pci_id, nhlt->header.oem_id, nhlt->header.oem_table_id,
  183. nhlt->header.oem_revision, "-tplg.bin");
  184. skl_nhlt_trim_space(skl);
  185. return 0;
  186. }