blob: e3f06672fd6df3268be67eb5790edcf91fa27585 [file] [log] [blame]
Jeeja KP473eb872015-07-21 23:53:55 +05301/*
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 */
Yong Zhif65cf7d62016-05-26 21:30:15 -070020#include <linux/pci.h>
Jeeja KP473eb872015-07-21 23:53:55 +053021#include "skl.h"
22
23/* Unique identification for getting NHLT blobs */
24static u8 OSC_UUID[16] = {0x6E, 0x88, 0x9F, 0xA6, 0xEB, 0x6C, 0x94, 0x45,
25 0xA4, 0x1F, 0x7B, 0x5D, 0xCE, 0x24, 0xC5, 0x53};
26
Jeeja KPc286b3f2016-05-05 11:19:19 +053027struct nhlt_acpi_table *skl_nhlt_init(struct device *dev)
Jeeja KP473eb872015-07-21 23:53:55 +053028{
29 acpi_handle handle;
30 union acpi_object *obj;
31 struct nhlt_resource_desc *nhlt_ptr = NULL;
Jeeja KPc286b3f2016-05-05 11:19:19 +053032 struct nhlt_acpi_table *nhlt_table = NULL;
Jeeja KP473eb872015-07-21 23:53:55 +053033
Vinod Koul6ad00052017-03-24 23:10:30 +053034 handle = ACPI_HANDLE(dev);
35 if (!handle) {
36 dev_err(dev, "Didn't find ACPI_HANDLE\n");
Jeeja KP473eb872015-07-21 23:53:55 +053037 return NULL;
38 }
39
40 obj = acpi_evaluate_dsm(handle, OSC_UUID, 1, 1, NULL);
41 if (obj && obj->type == ACPI_TYPE_BUFFER) {
42 nhlt_ptr = (struct nhlt_resource_desc *)obj->buffer.pointer;
Jeeja KPc286b3f2016-05-05 11:19:19 +053043 nhlt_table = (struct nhlt_acpi_table *)
44 memremap(nhlt_ptr->min_addr, nhlt_ptr->length,
Dan Williamsba40a852015-10-09 18:16:36 -040045 MEMREMAP_WB);
Jeeja KPc286b3f2016-05-05 11:19:19 +053046 ACPI_FREE(obj);
47 return nhlt_table;
Jeeja KP473eb872015-07-21 23:53:55 +053048 }
49
50 dev_err(dev, "device specific method to extract NHLT blob failed\n");
51 return NULL;
52}
53
Jeeja KPc286b3f2016-05-05 11:19:19 +053054void skl_nhlt_free(struct nhlt_acpi_table *nhlt)
Jeeja KP473eb872015-07-21 23:53:55 +053055{
Jeeja KPc286b3f2016-05-05 11:19:19 +053056 memunmap((void *) nhlt);
Jeeja KP473eb872015-07-21 23:53:55 +053057}
58
59static struct nhlt_specific_cfg *skl_get_specific_cfg(
60 struct device *dev, struct nhlt_fmt *fmt,
Jeeja KP16882d22015-10-27 09:22:58 +090061 u8 no_ch, u32 rate, u16 bps, u8 linktype)
Jeeja KP473eb872015-07-21 23:53:55 +053062{
63 struct nhlt_specific_cfg *sp_config;
64 struct wav_fmt *wfmt;
65 struct nhlt_fmt_cfg *fmt_config = fmt->fmt_config;
66 int i;
67
68 dev_dbg(dev, "Format count =%d\n", fmt->fmt_count);
69
70 for (i = 0; i < fmt->fmt_count; i++) {
71 wfmt = &fmt_config->fmt_ext.fmt;
72 dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", wfmt->channels,
73 wfmt->bits_per_sample, wfmt->samples_per_sec);
Jeeja KP16882d22015-10-27 09:22:58 +090074 if (wfmt->channels == no_ch && wfmt->bits_per_sample == bps) {
75 /*
76 * if link type is dmic ignore rate check as the blob is
77 * generic for all rates
78 */
Jeeja KP473eb872015-07-21 23:53:55 +053079 sp_config = &fmt_config->config;
Jeeja KP16882d22015-10-27 09:22:58 +090080 if (linktype == NHLT_LINK_DMIC)
81 return sp_config;
Jeeja KP473eb872015-07-21 23:53:55 +053082
Jeeja KP16882d22015-10-27 09:22:58 +090083 if (wfmt->samples_per_sec == rate)
84 return sp_config;
Jeeja KP473eb872015-07-21 23:53:55 +053085 }
86
87 fmt_config = (struct nhlt_fmt_cfg *)(fmt_config->config.caps +
88 fmt_config->config.size);
89 }
90
91 return NULL;
92}
93
94static void dump_config(struct device *dev, u32 instance_id, u8 linktype,
95 u8 s_fmt, u8 num_channels, u32 s_rate, u8 dirn, u16 bps)
96{
97 dev_dbg(dev, "Input configuration\n");
98 dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", num_channels, s_fmt, s_rate);
99 dev_dbg(dev, "vbus_id=%d link_type=%d\n", instance_id, linktype);
100 dev_dbg(dev, "bits_per_sample=%d\n", bps);
101}
102
103static bool skl_check_ep_match(struct device *dev, struct nhlt_endpoint *epnt,
Senthilnathan Veppurdb2f5862017-02-09 16:44:01 +0530104 u32 instance_id, u8 link_type, u8 dirn, u8 dev_type)
Jeeja KP473eb872015-07-21 23:53:55 +0530105{
Senthilnathan Veppurdb2f5862017-02-09 16:44:01 +0530106 dev_dbg(dev, "vbus_id=%d link_type=%d dir=%d dev_type = %d\n",
107 epnt->virtual_bus_id, epnt->linktype,
108 epnt->direction, epnt->device_type);
Jeeja KP473eb872015-07-21 23:53:55 +0530109
110 if ((epnt->virtual_bus_id == instance_id) &&
111 (epnt->linktype == link_type) &&
Senthilnathan Veppurdb2f5862017-02-09 16:44:01 +0530112 (epnt->direction == dirn) &&
113 (epnt->device_type == dev_type))
Jeeja KP473eb872015-07-21 23:53:55 +0530114 return true;
115 else
116 return false;
117}
118
119struct nhlt_specific_cfg
120*skl_get_ep_blob(struct skl *skl, u32 instance, u8 link_type,
Senthilnathan Veppurdb2f5862017-02-09 16:44:01 +0530121 u8 s_fmt, u8 num_ch, u32 s_rate,
122 u8 dirn, u8 dev_type)
Jeeja KP473eb872015-07-21 23:53:55 +0530123{
124 struct nhlt_fmt *fmt;
125 struct nhlt_endpoint *epnt;
126 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
127 struct device *dev = bus->dev;
128 struct nhlt_specific_cfg *sp_config;
Jeeja KPc286b3f2016-05-05 11:19:19 +0530129 struct nhlt_acpi_table *nhlt = skl->nhlt;
Jeeja KP83b50242015-10-27 09:22:50 +0900130 u16 bps = (s_fmt == 16) ? 16 : 32;
Jeeja KP473eb872015-07-21 23:53:55 +0530131 u8 j;
132
133 dump_config(dev, instance, link_type, s_fmt, num_ch, s_rate, dirn, bps);
134
135 epnt = (struct nhlt_endpoint *)nhlt->desc;
136
137 dev_dbg(dev, "endpoint count =%d\n", nhlt->endpoint_count);
138
139 for (j = 0; j < nhlt->endpoint_count; j++) {
Senthilnathan Veppurdb2f5862017-02-09 16:44:01 +0530140 if (skl_check_ep_match(dev, epnt, instance, link_type,
141 dirn, dev_type)) {
Jeeja KP473eb872015-07-21 23:53:55 +0530142 fmt = (struct nhlt_fmt *)(epnt->config.caps +
143 epnt->config.size);
Jeeja KP16882d22015-10-27 09:22:58 +0900144 sp_config = skl_get_specific_cfg(dev, fmt, num_ch,
145 s_rate, bps, link_type);
Jeeja KP473eb872015-07-21 23:53:55 +0530146 if (sp_config)
147 return sp_config;
148 }
149
150 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
151 }
152
153 return NULL;
154}
Vinod Koul4b235c42016-02-19 11:42:34 +0530155
Yong Zhif65cf7d62016-05-26 21:30:15 -0700156int skl_get_dmic_geo(struct skl *skl)
157{
158 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
159 struct nhlt_endpoint *epnt;
160 struct nhlt_dmic_array_config *cfg;
161 struct device *dev = &skl->pci->dev;
162 unsigned int dmic_geo = 0;
163 u8 j;
164
165 epnt = (struct nhlt_endpoint *)nhlt->desc;
166
167 for (j = 0; j < nhlt->endpoint_count; j++) {
168 if (epnt->linktype == NHLT_LINK_DMIC) {
169 cfg = (struct nhlt_dmic_array_config *)
170 (epnt->config.caps);
171 switch (cfg->array_type) {
172 case NHLT_MIC_ARRAY_2CH_SMALL:
173 case NHLT_MIC_ARRAY_2CH_BIG:
174 dmic_geo |= MIC_ARRAY_2CH;
175 break;
176
177 case NHLT_MIC_ARRAY_4CH_1ST_GEOM:
178 case NHLT_MIC_ARRAY_4CH_L_SHAPED:
179 case NHLT_MIC_ARRAY_4CH_2ND_GEOM:
180 dmic_geo |= MIC_ARRAY_4CH;
181 break;
182
183 default:
184 dev_warn(dev, "undefined DMIC array_type 0x%0x\n",
185 cfg->array_type);
186
187 }
188 }
189 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
190 }
191
192 return dmic_geo;
193}
194
Subhransu S. Prusty0cf5a172017-01-11 16:31:02 +0530195static void skl_nhlt_trim_space(char *trim)
Vinod Koul4b235c42016-02-19 11:42:34 +0530196{
Subhransu S. Prusty0cf5a172017-01-11 16:31:02 +0530197 char *s = trim;
Vinod Koul4b235c42016-02-19 11:42:34 +0530198 int cnt;
199 int i;
200
201 cnt = 0;
202 for (i = 0; s[i]; i++) {
203 if (!isspace(s[i]))
204 s[cnt++] = s[i];
205 }
206
207 s[cnt] = '\0';
208}
209
210int skl_nhlt_update_topology_bin(struct skl *skl)
211{
212 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
213 struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
214 struct device *dev = bus->dev;
215
216 dev_dbg(dev, "oem_id %.6s, oem_table_id %8s oem_revision %d\n",
217 nhlt->header.oem_id, nhlt->header.oem_table_id,
218 nhlt->header.oem_revision);
219
220 snprintf(skl->tplg_name, sizeof(skl->tplg_name), "%x-%.6s-%.8s-%d%s",
221 skl->pci_id, nhlt->header.oem_id, nhlt->header.oem_table_id,
222 nhlt->header.oem_revision, "-tplg.bin");
223
Subhransu S. Prusty0cf5a172017-01-11 16:31:02 +0530224 skl_nhlt_trim_space(skl->tplg_name);
Vinod Koul4b235c42016-02-19 11:42:34 +0530225
226 return 0;
227}
Subhransu S. Prusty0cf5a172017-01-11 16:31:02 +0530228
229static ssize_t skl_nhlt_platform_id_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
231{
232 struct pci_dev *pci = to_pci_dev(dev);
233 struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
234 struct skl *skl = ebus_to_skl(ebus);
235 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
236 char platform_id[32];
237
238 sprintf(platform_id, "%x-%.6s-%.8s-%d", skl->pci_id,
239 nhlt->header.oem_id, nhlt->header.oem_table_id,
240 nhlt->header.oem_revision);
241
242 skl_nhlt_trim_space(platform_id);
243 return sprintf(buf, "%s\n", platform_id);
244}
245
246static DEVICE_ATTR(platform_id, 0444, skl_nhlt_platform_id_show, NULL);
247
248int skl_nhlt_create_sysfs(struct skl *skl)
249{
250 struct device *dev = &skl->pci->dev;
251
252 if (sysfs_create_file(&dev->kobj, &dev_attr_platform_id.attr))
253 dev_warn(dev, "Error creating sysfs entry\n");
254
255 return 0;
256}
257
258void skl_nhlt_remove_sysfs(struct skl *skl)
259{
260 struct device *dev = &skl->pci->dev;
261
262 sysfs_remove_file(&dev->kobj, &dev_attr_platform_id.attr);
263}