2 * skl-nhlt.c - Intel SKL Platform NHLT parsing
4 * Copyright (C) 2015 Intel Corp
5 * Author: Sanjiv Kumar <sanjiv.kumar@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 #include <linux/pci.h>
23 #define NHLT_ACPI_HEADER_SIG "NHLT"
25 /* Unique identification for getting NHLT blobs */
26 static guid_t osc_guid =
27 GUID_INIT(0xA69F886E, 0x6CEB, 0x4594,
28 0xA4, 0x1F, 0x7B, 0x5D, 0xCE, 0x24, 0xC5, 0x53);
30 struct nhlt_acpi_table *skl_nhlt_init(struct device *dev)
33 union acpi_object *obj;
34 struct nhlt_resource_desc *nhlt_ptr = NULL;
35 struct nhlt_acpi_table *nhlt_table = NULL;
37 handle = ACPI_HANDLE(dev);
39 dev_err(dev, "Didn't find ACPI_HANDLE\n");
43 obj = acpi_evaluate_dsm(handle, &osc_guid, 1, 1, NULL);
44 if (obj && obj->type == ACPI_TYPE_BUFFER) {
45 nhlt_ptr = (struct nhlt_resource_desc *)obj->buffer.pointer;
46 nhlt_table = (struct nhlt_acpi_table *)
47 memremap(nhlt_ptr->min_addr, nhlt_ptr->length,
50 if (nhlt_table && (strncmp(nhlt_table->header.signature,
52 strlen(NHLT_ACPI_HEADER_SIG)) != 0)) {
54 dev_err(dev, "NHLT ACPI header signature incorrect\n");
60 dev_err(dev, "device specific method to extract NHLT blob failed\n");
64 void skl_nhlt_free(struct nhlt_acpi_table *nhlt)
66 memunmap((void *) nhlt);
69 static struct nhlt_specific_cfg *skl_get_specific_cfg(
70 struct device *dev, struct nhlt_fmt *fmt,
71 u8 no_ch, u32 rate, u16 bps, u8 linktype)
73 struct nhlt_specific_cfg *sp_config;
75 struct nhlt_fmt_cfg *fmt_config = fmt->fmt_config;
78 dev_dbg(dev, "Format count =%d\n", fmt->fmt_count);
80 for (i = 0; i < fmt->fmt_count; i++) {
81 wfmt = &fmt_config->fmt_ext.fmt;
82 dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", wfmt->channels,
83 wfmt->bits_per_sample, wfmt->samples_per_sec);
84 if (wfmt->channels == no_ch && wfmt->bits_per_sample == bps) {
86 * if link type is dmic ignore rate check as the blob is
87 * generic for all rates
89 sp_config = &fmt_config->config;
90 if (linktype == NHLT_LINK_DMIC)
93 if (wfmt->samples_per_sec == rate)
97 fmt_config = (struct nhlt_fmt_cfg *)(fmt_config->config.caps +
98 fmt_config->config.size);
104 static void dump_config(struct device *dev, u32 instance_id, u8 linktype,
105 u8 s_fmt, u8 num_channels, u32 s_rate, u8 dirn, u16 bps)
107 dev_dbg(dev, "Input configuration\n");
108 dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", num_channels, s_fmt, s_rate);
109 dev_dbg(dev, "vbus_id=%d link_type=%d\n", instance_id, linktype);
110 dev_dbg(dev, "bits_per_sample=%d\n", bps);
113 static bool skl_check_ep_match(struct device *dev, struct nhlt_endpoint *epnt,
114 u32 instance_id, u8 link_type, u8 dirn, u8 dev_type)
116 dev_dbg(dev, "vbus_id=%d link_type=%d dir=%d dev_type = %d\n",
117 epnt->virtual_bus_id, epnt->linktype,
118 epnt->direction, epnt->device_type);
120 if ((epnt->virtual_bus_id == instance_id) &&
121 (epnt->linktype == link_type) &&
122 (epnt->direction == dirn) &&
123 (epnt->device_type == dev_type))
129 struct nhlt_specific_cfg
130 *skl_get_ep_blob(struct skl *skl, u32 instance, u8 link_type,
131 u8 s_fmt, u8 num_ch, u32 s_rate,
132 u8 dirn, u8 dev_type)
134 struct nhlt_fmt *fmt;
135 struct nhlt_endpoint *epnt;
136 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
137 struct device *dev = bus->dev;
138 struct nhlt_specific_cfg *sp_config;
139 struct nhlt_acpi_table *nhlt = skl->nhlt;
140 u16 bps = (s_fmt == 16) ? 16 : 32;
143 dump_config(dev, instance, link_type, s_fmt, num_ch, s_rate, dirn, bps);
145 epnt = (struct nhlt_endpoint *)nhlt->desc;
147 dev_dbg(dev, "endpoint count =%d\n", nhlt->endpoint_count);
149 for (j = 0; j < nhlt->endpoint_count; j++) {
150 if (skl_check_ep_match(dev, epnt, instance, link_type,
152 fmt = (struct nhlt_fmt *)(epnt->config.caps +
154 sp_config = skl_get_specific_cfg(dev, fmt, num_ch,
155 s_rate, bps, link_type);
160 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
166 int skl_get_dmic_geo(struct skl *skl)
168 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
169 struct nhlt_endpoint *epnt;
170 struct nhlt_dmic_array_config *cfg;
171 struct device *dev = &skl->pci->dev;
172 unsigned int dmic_geo = 0;
175 epnt = (struct nhlt_endpoint *)nhlt->desc;
177 for (j = 0; j < nhlt->endpoint_count; j++) {
178 if (epnt->linktype == NHLT_LINK_DMIC) {
179 cfg = (struct nhlt_dmic_array_config *)
181 switch (cfg->array_type) {
182 case NHLT_MIC_ARRAY_2CH_SMALL:
183 case NHLT_MIC_ARRAY_2CH_BIG:
184 dmic_geo |= MIC_ARRAY_2CH;
187 case NHLT_MIC_ARRAY_4CH_1ST_GEOM:
188 case NHLT_MIC_ARRAY_4CH_L_SHAPED:
189 case NHLT_MIC_ARRAY_4CH_2ND_GEOM:
190 dmic_geo |= MIC_ARRAY_4CH;
194 dev_warn(dev, "undefined DMIC array_type 0x%0x\n",
199 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
205 static void skl_nhlt_trim_space(char *trim)
212 for (i = 0; s[i]; i++) {
220 int skl_nhlt_update_topology_bin(struct skl *skl)
222 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
223 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
224 struct device *dev = bus->dev;
226 dev_dbg(dev, "oem_id %.6s, oem_table_id %8s oem_revision %d\n",
227 nhlt->header.oem_id, nhlt->header.oem_table_id,
228 nhlt->header.oem_revision);
230 snprintf(skl->tplg_name, sizeof(skl->tplg_name), "%x-%.6s-%.8s-%d%s",
231 skl->pci_id, nhlt->header.oem_id, nhlt->header.oem_table_id,
232 nhlt->header.oem_revision, "-tplg.bin");
234 skl_nhlt_trim_space(skl->tplg_name);
239 static ssize_t skl_nhlt_platform_id_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
242 struct pci_dev *pci = to_pci_dev(dev);
243 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
244 struct skl *skl = ebus_to_skl(ebus);
245 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
246 char platform_id[32];
248 sprintf(platform_id, "%x-%.6s-%.8s-%d", skl->pci_id,
249 nhlt->header.oem_id, nhlt->header.oem_table_id,
250 nhlt->header.oem_revision);
252 skl_nhlt_trim_space(platform_id);
253 return sprintf(buf, "%s\n", platform_id);
256 static DEVICE_ATTR(platform_id, 0444, skl_nhlt_platform_id_show, NULL);
258 int skl_nhlt_create_sysfs(struct skl *skl)
260 struct device *dev = &skl->pci->dev;
262 if (sysfs_create_file(&dev->kobj, &dev_attr_platform_id.attr))
263 dev_warn(dev, "Error creating sysfs entry\n");
268 void skl_nhlt_remove_sysfs(struct skl *skl)
270 struct device *dev = &skl->pci->dev;
272 sysfs_remove_file(&dev->kobj, &dev_attr_platform_id.attr);