]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - sound/soc/intel/skylake/skl-nhlt.c
8ae61666adf4170260b3c7c1d9e8d32af0c7b135
[mirror_ubuntu-eoan-kernel.git] / sound / soc / intel / skylake / skl-nhlt.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * skl-nhlt.c - Intel SKL Platform NHLT parsing
4 *
5 * Copyright (C) 2015 Intel Corp
6 * Author: Sanjiv Kumar <sanjiv.kumar@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 */
11 #include <linux/pci.h>
12 #include <sound/intel-nhlt.h>
13 #include "skl.h"
14 #include "skl-i2s.h"
15
16 #define NHLT_ACPI_HEADER_SIG "NHLT"
17
18 /* Unique identification for getting NHLT blobs */
19 static guid_t osc_guid =
20 GUID_INIT(0xA69F886E, 0x6CEB, 0x4594,
21 0xA4, 0x1F, 0x7B, 0x5D, 0xCE, 0x24, 0xC5, 0x53);
22
23
24 struct nhlt_acpi_table *skl_nhlt_init(struct device *dev)
25 {
26 acpi_handle handle;
27 union acpi_object *obj;
28 struct nhlt_resource_desc *nhlt_ptr = NULL;
29 struct nhlt_acpi_table *nhlt_table = NULL;
30
31 handle = ACPI_HANDLE(dev);
32 if (!handle) {
33 dev_err(dev, "Didn't find ACPI_HANDLE\n");
34 return NULL;
35 }
36
37 obj = acpi_evaluate_dsm(handle, &osc_guid, 1, 1, NULL);
38 if (obj && obj->type == ACPI_TYPE_BUFFER) {
39 nhlt_ptr = (struct nhlt_resource_desc *)obj->buffer.pointer;
40 if (nhlt_ptr->length)
41 nhlt_table = (struct nhlt_acpi_table *)
42 memremap(nhlt_ptr->min_addr, nhlt_ptr->length,
43 MEMREMAP_WB);
44 ACPI_FREE(obj);
45 if (nhlt_table && (strncmp(nhlt_table->header.signature,
46 NHLT_ACPI_HEADER_SIG,
47 strlen(NHLT_ACPI_HEADER_SIG)) != 0)) {
48 memunmap(nhlt_table);
49 dev_err(dev, "NHLT ACPI header signature incorrect\n");
50 return NULL;
51 }
52 return nhlt_table;
53 }
54
55 dev_err(dev, "device specific method to extract NHLT blob failed\n");
56 return NULL;
57 }
58
59 void skl_nhlt_free(struct nhlt_acpi_table *nhlt)
60 {
61 memunmap((void *) nhlt);
62 }
63
64 static struct nhlt_specific_cfg *skl_get_specific_cfg(
65 struct device *dev, struct nhlt_fmt *fmt,
66 u8 no_ch, u32 rate, u16 bps, u8 linktype)
67 {
68 struct nhlt_specific_cfg *sp_config;
69 struct wav_fmt *wfmt;
70 struct nhlt_fmt_cfg *fmt_config = fmt->fmt_config;
71 int i;
72
73 dev_dbg(dev, "Format count =%d\n", fmt->fmt_count);
74
75 for (i = 0; i < fmt->fmt_count; i++) {
76 wfmt = &fmt_config->fmt_ext.fmt;
77 dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", wfmt->channels,
78 wfmt->bits_per_sample, wfmt->samples_per_sec);
79 if (wfmt->channels == no_ch && wfmt->bits_per_sample == bps) {
80 /*
81 * if link type is dmic ignore rate check as the blob is
82 * generic for all rates
83 */
84 sp_config = &fmt_config->config;
85 if (linktype == NHLT_LINK_DMIC)
86 return sp_config;
87
88 if (wfmt->samples_per_sec == rate)
89 return sp_config;
90 }
91
92 fmt_config = (struct nhlt_fmt_cfg *)(fmt_config->config.caps +
93 fmt_config->config.size);
94 }
95
96 return NULL;
97 }
98
99 static void dump_config(struct device *dev, u32 instance_id, u8 linktype,
100 u8 s_fmt, u8 num_channels, u32 s_rate, u8 dirn, u16 bps)
101 {
102 dev_dbg(dev, "Input configuration\n");
103 dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", num_channels, s_fmt, s_rate);
104 dev_dbg(dev, "vbus_id=%d link_type=%d\n", instance_id, linktype);
105 dev_dbg(dev, "bits_per_sample=%d\n", bps);
106 }
107
108 static bool skl_check_ep_match(struct device *dev, struct nhlt_endpoint *epnt,
109 u32 instance_id, u8 link_type, u8 dirn, u8 dev_type)
110 {
111 dev_dbg(dev, "vbus_id=%d link_type=%d dir=%d dev_type = %d\n",
112 epnt->virtual_bus_id, epnt->linktype,
113 epnt->direction, epnt->device_type);
114
115 if ((epnt->virtual_bus_id == instance_id) &&
116 (epnt->linktype == link_type) &&
117 (epnt->direction == dirn)) {
118 /* do not check dev_type for DMIC link type */
119 if (epnt->linktype == NHLT_LINK_DMIC)
120 return true;
121
122 if (epnt->device_type == dev_type)
123 return true;
124 }
125
126 return false;
127 }
128
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)
133 {
134 struct nhlt_fmt *fmt;
135 struct nhlt_endpoint *epnt;
136 struct hdac_bus *bus = skl_to_bus(skl);
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;
141 u8 j;
142
143 dump_config(dev, instance, link_type, s_fmt, num_ch, s_rate, dirn, bps);
144
145 epnt = (struct nhlt_endpoint *)nhlt->desc;
146
147 dev_dbg(dev, "endpoint count =%d\n", nhlt->endpoint_count);
148
149 for (j = 0; j < nhlt->endpoint_count; j++) {
150 if (skl_check_ep_match(dev, epnt, instance, link_type,
151 dirn, dev_type)) {
152 fmt = (struct nhlt_fmt *)(epnt->config.caps +
153 epnt->config.size);
154 sp_config = skl_get_specific_cfg(dev, fmt, num_ch,
155 s_rate, bps, link_type);
156 if (sp_config)
157 return sp_config;
158 }
159
160 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
161 }
162
163 return NULL;
164 }
165
166 int skl_get_dmic_geo(struct skl *skl)
167 {
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;
173 u8 j;
174
175 if (!nhlt)
176 return 0;
177
178 epnt = (struct nhlt_endpoint *)nhlt->desc;
179
180 for (j = 0; j < nhlt->endpoint_count; j++) {
181 if (epnt->linktype == NHLT_LINK_DMIC) {
182 cfg = (struct nhlt_dmic_array_config *)
183 (epnt->config.caps);
184 switch (cfg->array_type) {
185 case NHLT_MIC_ARRAY_2CH_SMALL:
186 case NHLT_MIC_ARRAY_2CH_BIG:
187 dmic_geo |= MIC_ARRAY_2CH;
188 break;
189
190 case NHLT_MIC_ARRAY_4CH_1ST_GEOM:
191 case NHLT_MIC_ARRAY_4CH_L_SHAPED:
192 case NHLT_MIC_ARRAY_4CH_2ND_GEOM:
193 dmic_geo |= MIC_ARRAY_4CH;
194 break;
195
196 default:
197 dev_warn(dev, "undefined DMIC array_type 0x%0x\n",
198 cfg->array_type);
199
200 }
201 }
202 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
203 }
204
205 return dmic_geo;
206 }
207
208 static void skl_nhlt_trim_space(char *trim)
209 {
210 char *s = trim;
211 int cnt;
212 int i;
213
214 cnt = 0;
215 for (i = 0; s[i]; i++) {
216 if (!isspace(s[i]))
217 s[cnt++] = s[i];
218 }
219
220 s[cnt] = '\0';
221 }
222
223 int skl_nhlt_update_topology_bin(struct skl *skl)
224 {
225 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
226 struct hdac_bus *bus = skl_to_bus(skl);
227 struct device *dev = bus->dev;
228
229 dev_dbg(dev, "oem_id %.6s, oem_table_id %.8s oem_revision %d\n",
230 nhlt->header.oem_id, nhlt->header.oem_table_id,
231 nhlt->header.oem_revision);
232
233 snprintf(skl->tplg_name, sizeof(skl->tplg_name), "%x-%.6s-%.8s-%d%s",
234 skl->pci_id, nhlt->header.oem_id, nhlt->header.oem_table_id,
235 nhlt->header.oem_revision, "-tplg.bin");
236
237 skl_nhlt_trim_space(skl->tplg_name);
238
239 return 0;
240 }
241
242 static ssize_t skl_nhlt_platform_id_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
244 {
245 struct pci_dev *pci = to_pci_dev(dev);
246 struct hdac_bus *bus = pci_get_drvdata(pci);
247 struct skl *skl = bus_to_skl(bus);
248 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
249 char platform_id[32];
250
251 sprintf(platform_id, "%x-%.6s-%.8s-%d", skl->pci_id,
252 nhlt->header.oem_id, nhlt->header.oem_table_id,
253 nhlt->header.oem_revision);
254
255 skl_nhlt_trim_space(platform_id);
256 return sprintf(buf, "%s\n", platform_id);
257 }
258
259 static DEVICE_ATTR(platform_id, 0444, skl_nhlt_platform_id_show, NULL);
260
261 int skl_nhlt_create_sysfs(struct skl *skl)
262 {
263 struct device *dev = &skl->pci->dev;
264
265 if (sysfs_create_file(&dev->kobj, &dev_attr_platform_id.attr))
266 dev_warn(dev, "Error creating sysfs entry\n");
267
268 return 0;
269 }
270
271 void skl_nhlt_remove_sysfs(struct skl *skl)
272 {
273 struct device *dev = &skl->pci->dev;
274
275 sysfs_remove_file(&dev->kobj, &dev_attr_platform_id.attr);
276 }
277
278 /*
279 * Queries NHLT for all the fmt configuration for a particular endpoint and
280 * stores all possible rates supported in a rate table for the corresponding
281 * sclk/sclkfs.
282 */
283 static void skl_get_ssp_clks(struct skl *skl, struct skl_ssp_clk *ssp_clks,
284 struct nhlt_fmt *fmt, u8 id)
285 {
286 struct skl_i2s_config_blob_ext *i2s_config_ext;
287 struct skl_i2s_config_blob_legacy *i2s_config;
288 struct skl_clk_parent_src *parent;
289 struct skl_ssp_clk *sclk, *sclkfs;
290 struct nhlt_fmt_cfg *fmt_cfg;
291 struct wav_fmt_ext *wav_fmt;
292 unsigned long rate = 0;
293 bool present = false;
294 int rate_index = 0;
295 u16 channels, bps;
296 u8 clk_src;
297 int i, j;
298 u32 fs;
299
300 sclk = &ssp_clks[SKL_SCLK_OFS];
301 sclkfs = &ssp_clks[SKL_SCLKFS_OFS];
302
303 if (fmt->fmt_count == 0)
304 return;
305
306 for (i = 0; i < fmt->fmt_count; i++) {
307 fmt_cfg = &fmt->fmt_config[i];
308 wav_fmt = &fmt_cfg->fmt_ext;
309
310 channels = wav_fmt->fmt.channels;
311 bps = wav_fmt->fmt.bits_per_sample;
312 fs = wav_fmt->fmt.samples_per_sec;
313
314 /*
315 * In case of TDM configuration on a ssp, there can
316 * be more than one blob in which channel masks are
317 * different for each usecase for a specific rate and bps.
318 * But the sclk rate will be generated for the total
319 * number of channels used for that endpoint.
320 *
321 * So for the given fs and bps, choose blob which has
322 * the superset of all channels for that endpoint and
323 * derive the rate.
324 */
325 for (j = i; j < fmt->fmt_count; j++) {
326 fmt_cfg = &fmt->fmt_config[j];
327 wav_fmt = &fmt_cfg->fmt_ext;
328 if ((fs == wav_fmt->fmt.samples_per_sec) &&
329 (bps == wav_fmt->fmt.bits_per_sample))
330 channels = max_t(u16, channels,
331 wav_fmt->fmt.channels);
332 }
333
334 rate = channels * bps * fs;
335
336 /* check if the rate is added already to the given SSP's sclk */
337 for (j = 0; (j < SKL_MAX_CLK_RATES) &&
338 (sclk[id].rate_cfg[j].rate != 0); j++) {
339 if (sclk[id].rate_cfg[j].rate == rate) {
340 present = true;
341 break;
342 }
343 }
344
345 /* Fill rate and parent for sclk/sclkfs */
346 if (!present) {
347 i2s_config_ext = (struct skl_i2s_config_blob_ext *)
348 fmt->fmt_config[0].config.caps;
349
350 /* MCLK Divider Source Select */
351 if (is_legacy_blob(i2s_config_ext->hdr.sig)) {
352 i2s_config = ext_to_legacy_blob(i2s_config_ext);
353 clk_src = get_clk_src(i2s_config->mclk,
354 SKL_MNDSS_DIV_CLK_SRC_MASK);
355 } else {
356 clk_src = get_clk_src(i2s_config_ext->mclk,
357 SKL_MNDSS_DIV_CLK_SRC_MASK);
358 }
359
360 parent = skl_get_parent_clk(clk_src);
361
362 /*
363 * Do not copy the config data if there is no parent
364 * clock available for this clock source select
365 */
366 if (!parent)
367 continue;
368
369 sclk[id].rate_cfg[rate_index].rate = rate;
370 sclk[id].rate_cfg[rate_index].config = fmt_cfg;
371 sclkfs[id].rate_cfg[rate_index].rate = rate;
372 sclkfs[id].rate_cfg[rate_index].config = fmt_cfg;
373 sclk[id].parent_name = parent->name;
374 sclkfs[id].parent_name = parent->name;
375
376 rate_index++;
377 }
378 }
379 }
380
381 static void skl_get_mclk(struct skl *skl, struct skl_ssp_clk *mclk,
382 struct nhlt_fmt *fmt, u8 id)
383 {
384 struct skl_i2s_config_blob_ext *i2s_config_ext;
385 struct skl_i2s_config_blob_legacy *i2s_config;
386 struct nhlt_specific_cfg *fmt_cfg;
387 struct skl_clk_parent_src *parent;
388 u32 clkdiv, div_ratio;
389 u8 clk_src;
390
391 fmt_cfg = &fmt->fmt_config[0].config;
392 i2s_config_ext = (struct skl_i2s_config_blob_ext *)fmt_cfg->caps;
393
394 /* MCLK Divider Source Select and divider */
395 if (is_legacy_blob(i2s_config_ext->hdr.sig)) {
396 i2s_config = ext_to_legacy_blob(i2s_config_ext);
397 clk_src = get_clk_src(i2s_config->mclk,
398 SKL_MCLK_DIV_CLK_SRC_MASK);
399 clkdiv = i2s_config->mclk.mdivr &
400 SKL_MCLK_DIV_RATIO_MASK;
401 } else {
402 clk_src = get_clk_src(i2s_config_ext->mclk,
403 SKL_MCLK_DIV_CLK_SRC_MASK);
404 clkdiv = i2s_config_ext->mclk.mdivr[0] &
405 SKL_MCLK_DIV_RATIO_MASK;
406 }
407
408 /* bypass divider */
409 div_ratio = 1;
410
411 if (clkdiv != SKL_MCLK_DIV_RATIO_MASK)
412 /* Divider is 2 + clkdiv */
413 div_ratio = clkdiv + 2;
414
415 /* Calculate MCLK rate from source using div value */
416 parent = skl_get_parent_clk(clk_src);
417 if (!parent)
418 return;
419
420 mclk[id].rate_cfg[0].rate = parent->rate/div_ratio;
421 mclk[id].rate_cfg[0].config = &fmt->fmt_config[0];
422 mclk[id].parent_name = parent->name;
423 }
424
425 void skl_get_clks(struct skl *skl, struct skl_ssp_clk *ssp_clks)
426 {
427 struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
428 struct nhlt_endpoint *epnt;
429 struct nhlt_fmt *fmt;
430 int i;
431 u8 id;
432
433 epnt = (struct nhlt_endpoint *)nhlt->desc;
434 for (i = 0; i < nhlt->endpoint_count; i++) {
435 if (epnt->linktype == NHLT_LINK_SSP) {
436 id = epnt->virtual_bus_id;
437
438 fmt = (struct nhlt_fmt *)(epnt->config.caps
439 + epnt->config.size);
440
441 skl_get_ssp_clks(skl, ssp_clks, fmt, id);
442 skl_get_mclk(skl, ssp_clks, fmt, id);
443 }
444 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
445 }
446 }