1 // SPDX-License-Identifier: GPL-2.0-only
3 * skl-nhlt.c - Intel SKL Platform NHLT parsing
5 * Copyright (C) 2015 Intel Corp
6 * Author: Sanjiv Kumar <sanjiv.kumar@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 #include <linux/pci.h>
12 #include <sound/intel-nhlt.h>
16 #define NHLT_ACPI_HEADER_SIG "NHLT"
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);
24 struct nhlt_acpi_table
*skl_nhlt_init(struct device
*dev
)
27 union acpi_object
*obj
;
28 struct nhlt_resource_desc
*nhlt_ptr
= NULL
;
29 struct nhlt_acpi_table
*nhlt_table
= NULL
;
31 handle
= ACPI_HANDLE(dev
);
33 dev_err(dev
, "Didn't find ACPI_HANDLE\n");
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
;
41 nhlt_table
= (struct nhlt_acpi_table
*)
42 memremap(nhlt_ptr
->min_addr
, nhlt_ptr
->length
,
45 if (nhlt_table
&& (strncmp(nhlt_table
->header
.signature
,
47 strlen(NHLT_ACPI_HEADER_SIG
)) != 0)) {
49 dev_err(dev
, "NHLT ACPI header signature incorrect\n");
55 dev_err(dev
, "device specific method to extract NHLT blob failed\n");
59 void skl_nhlt_free(struct nhlt_acpi_table
*nhlt
)
61 memunmap((void *) nhlt
);
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
)
68 struct nhlt_specific_cfg
*sp_config
;
70 struct nhlt_fmt_cfg
*fmt_config
= fmt
->fmt_config
;
73 dev_dbg(dev
, "Format count =%d\n", fmt
->fmt_count
);
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
) {
81 * if link type is dmic ignore rate check as the blob is
82 * generic for all rates
84 sp_config
= &fmt_config
->config
;
85 if (linktype
== NHLT_LINK_DMIC
)
88 if (wfmt
->samples_per_sec
== rate
)
92 fmt_config
= (struct nhlt_fmt_cfg
*)(fmt_config
->config
.caps
+
93 fmt_config
->config
.size
);
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
)
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
);
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
)
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
);
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
)
122 if (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
= 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;
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;
178 epnt
= (struct nhlt_endpoint
*)nhlt
->desc
;
180 for (j
= 0; j
< nhlt
->endpoint_count
; j
++) {
181 if (epnt
->linktype
== NHLT_LINK_DMIC
) {
182 cfg
= (struct nhlt_dmic_array_config
*)
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
;
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
;
197 dev_warn(dev
, "undefined DMIC array_type 0x%0x\n",
202 epnt
= (struct nhlt_endpoint
*)((u8
*)epnt
+ epnt
->length
);
208 static void skl_nhlt_trim_space(char *trim
)
215 for (i
= 0; s
[i
]; i
++) {
223 int skl_nhlt_update_topology_bin(struct skl
*skl
)
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
;
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
);
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");
237 skl_nhlt_trim_space(skl
->tplg_name
);
242 static ssize_t
skl_nhlt_platform_id_show(struct device
*dev
,
243 struct device_attribute
*attr
, char *buf
)
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];
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
);
255 skl_nhlt_trim_space(platform_id
);
256 return sprintf(buf
, "%s\n", platform_id
);
259 static DEVICE_ATTR(platform_id
, 0444, skl_nhlt_platform_id_show
, NULL
);
261 int skl_nhlt_create_sysfs(struct skl
*skl
)
263 struct device
*dev
= &skl
->pci
->dev
;
265 if (sysfs_create_file(&dev
->kobj
, &dev_attr_platform_id
.attr
))
266 dev_warn(dev
, "Error creating sysfs entry\n");
271 void skl_nhlt_remove_sysfs(struct skl
*skl
)
273 struct device
*dev
= &skl
->pci
->dev
;
275 sysfs_remove_file(&dev
->kobj
, &dev_attr_platform_id
.attr
);
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
283 static void skl_get_ssp_clks(struct skl
*skl
, struct skl_ssp_clk
*ssp_clks
,
284 struct nhlt_fmt
*fmt
, u8 id
)
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;
300 sclk
= &ssp_clks
[SKL_SCLK_OFS
];
301 sclkfs
= &ssp_clks
[SKL_SCLKFS_OFS
];
303 if (fmt
->fmt_count
== 0)
306 for (i
= 0; i
< fmt
->fmt_count
; i
++) {
307 fmt_cfg
= &fmt
->fmt_config
[i
];
308 wav_fmt
= &fmt_cfg
->fmt_ext
;
310 channels
= wav_fmt
->fmt
.channels
;
311 bps
= wav_fmt
->fmt
.bits_per_sample
;
312 fs
= wav_fmt
->fmt
.samples_per_sec
;
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.
321 * So for the given fs and bps, choose blob which has
322 * the superset of all channels for that endpoint and
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
);
334 rate
= channels
* bps
* fs
;
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
) {
345 /* Fill rate and parent for sclk/sclkfs */
347 i2s_config_ext
= (struct skl_i2s_config_blob_ext
*)
348 fmt
->fmt_config
[0].config
.caps
;
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
);
356 clk_src
= get_clk_src(i2s_config_ext
->mclk
,
357 SKL_MNDSS_DIV_CLK_SRC_MASK
);
360 parent
= skl_get_parent_clk(clk_src
);
363 * Do not copy the config data if there is no parent
364 * clock available for this clock source select
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
;
381 static void skl_get_mclk(struct skl
*skl
, struct skl_ssp_clk
*mclk
,
382 struct nhlt_fmt
*fmt
, u8 id
)
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
;
391 fmt_cfg
= &fmt
->fmt_config
[0].config
;
392 i2s_config_ext
= (struct skl_i2s_config_blob_ext
*)fmt_cfg
->caps
;
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
;
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
;
411 if (clkdiv
!= SKL_MCLK_DIV_RATIO_MASK
)
412 /* Divider is 2 + clkdiv */
413 div_ratio
= clkdiv
+ 2;
415 /* Calculate MCLK rate from source using div value */
416 parent
= skl_get_parent_clk(clk_src
);
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
;
425 void skl_get_clks(struct skl
*skl
, struct skl_ssp_clk
*ssp_clks
)
427 struct nhlt_acpi_table
*nhlt
= (struct nhlt_acpi_table
*)skl
->nhlt
;
428 struct nhlt_endpoint
*epnt
;
429 struct nhlt_fmt
*fmt
;
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
;
438 fmt
= (struct nhlt_fmt
*)(epnt
->config
.caps
439 + epnt
->config
.size
);
441 skl_get_ssp_clks(skl
, ssp_clks
, fmt
, id
);
442 skl_get_mclk(skl
, ssp_clks
, fmt
, id
);
444 epnt
= (struct nhlt_endpoint
*)((u8
*)epnt
+ epnt
->length
);