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>
24 #define NHLT_ACPI_HEADER_SIG "NHLT"
26 /* Unique identification for getting NHLT blobs */
27 static guid_t osc_guid
=
28 GUID_INIT(0xA69F886E, 0x6CEB, 0x4594,
29 0xA4, 0x1F, 0x7B, 0x5D, 0xCE, 0x24, 0xC5, 0x53);
31 struct nhlt_acpi_table
*skl_nhlt_init(struct device
*dev
)
34 union acpi_object
*obj
;
35 struct nhlt_resource_desc
*nhlt_ptr
= NULL
;
36 struct nhlt_acpi_table
*nhlt_table
= NULL
;
38 handle
= ACPI_HANDLE(dev
);
40 dev_err(dev
, "Didn't find ACPI_HANDLE\n");
44 obj
= acpi_evaluate_dsm(handle
, &osc_guid
, 1, 1, NULL
);
45 if (obj
&& obj
->type
== ACPI_TYPE_BUFFER
) {
46 nhlt_ptr
= (struct nhlt_resource_desc
*)obj
->buffer
.pointer
;
48 nhlt_table
= (struct nhlt_acpi_table
*)
49 memremap(nhlt_ptr
->min_addr
, nhlt_ptr
->length
,
52 if (nhlt_table
&& (strncmp(nhlt_table
->header
.signature
,
54 strlen(NHLT_ACPI_HEADER_SIG
)) != 0)) {
56 dev_err(dev
, "NHLT ACPI header signature incorrect\n");
62 dev_err(dev
, "device specific method to extract NHLT blob failed\n");
66 void skl_nhlt_free(struct nhlt_acpi_table
*nhlt
)
68 memunmap((void *) nhlt
);
71 static struct nhlt_specific_cfg
*skl_get_specific_cfg(
72 struct device
*dev
, struct nhlt_fmt
*fmt
,
73 u8 no_ch
, u32 rate
, u16 bps
, u8 linktype
)
75 struct nhlt_specific_cfg
*sp_config
;
77 struct nhlt_fmt_cfg
*fmt_config
= fmt
->fmt_config
;
80 dev_dbg(dev
, "Format count =%d\n", fmt
->fmt_count
);
82 for (i
= 0; i
< fmt
->fmt_count
; i
++) {
83 wfmt
= &fmt_config
->fmt_ext
.fmt
;
84 dev_dbg(dev
, "ch=%d fmt=%d s_rate=%d\n", wfmt
->channels
,
85 wfmt
->bits_per_sample
, wfmt
->samples_per_sec
);
86 if (wfmt
->channels
== no_ch
&& wfmt
->bits_per_sample
== bps
) {
88 * if link type is dmic ignore rate check as the blob is
89 * generic for all rates
91 sp_config
= &fmt_config
->config
;
92 if (linktype
== NHLT_LINK_DMIC
)
95 if (wfmt
->samples_per_sec
== rate
)
99 fmt_config
= (struct nhlt_fmt_cfg
*)(fmt_config
->config
.caps
+
100 fmt_config
->config
.size
);
106 static void dump_config(struct device
*dev
, u32 instance_id
, u8 linktype
,
107 u8 s_fmt
, u8 num_channels
, u32 s_rate
, u8 dirn
, u16 bps
)
109 dev_dbg(dev
, "Input configuration\n");
110 dev_dbg(dev
, "ch=%d fmt=%d s_rate=%d\n", num_channels
, s_fmt
, s_rate
);
111 dev_dbg(dev
, "vbus_id=%d link_type=%d\n", instance_id
, linktype
);
112 dev_dbg(dev
, "bits_per_sample=%d\n", bps
);
115 static bool skl_check_ep_match(struct device
*dev
, struct nhlt_endpoint
*epnt
,
116 u32 instance_id
, u8 link_type
, u8 dirn
, u8 dev_type
)
118 dev_dbg(dev
, "vbus_id=%d link_type=%d dir=%d dev_type = %d\n",
119 epnt
->virtual_bus_id
, epnt
->linktype
,
120 epnt
->direction
, epnt
->device_type
);
122 if ((epnt
->virtual_bus_id
== instance_id
) &&
123 (epnt
->linktype
== link_type
) &&
124 (epnt
->direction
== dirn
)) {
125 /* do not check dev_type for DMIC link type */
126 if (epnt
->linktype
== NHLT_LINK_DMIC
)
129 if (epnt
->device_type
== dev_type
)
136 struct nhlt_specific_cfg
137 *skl_get_ep_blob(struct skl
*skl
, u32 instance
, u8 link_type
,
138 u8 s_fmt
, u8 num_ch
, u32 s_rate
,
139 u8 dirn
, u8 dev_type
)
141 struct nhlt_fmt
*fmt
;
142 struct nhlt_endpoint
*epnt
;
143 struct hdac_bus
*bus
= ebus_to_hbus(&skl
->ebus
);
144 struct device
*dev
= bus
->dev
;
145 struct nhlt_specific_cfg
*sp_config
;
146 struct nhlt_acpi_table
*nhlt
= skl
->nhlt
;
147 u16 bps
= (s_fmt
== 16) ? 16 : 32;
150 dump_config(dev
, instance
, link_type
, s_fmt
, num_ch
, s_rate
, dirn
, bps
);
152 epnt
= (struct nhlt_endpoint
*)nhlt
->desc
;
154 dev_dbg(dev
, "endpoint count =%d\n", nhlt
->endpoint_count
);
156 for (j
= 0; j
< nhlt
->endpoint_count
; j
++) {
157 if (skl_check_ep_match(dev
, epnt
, instance
, link_type
,
159 fmt
= (struct nhlt_fmt
*)(epnt
->config
.caps
+
161 sp_config
= skl_get_specific_cfg(dev
, fmt
, num_ch
,
162 s_rate
, bps
, link_type
);
167 epnt
= (struct nhlt_endpoint
*)((u8
*)epnt
+ epnt
->length
);
173 int skl_get_dmic_geo(struct skl
*skl
)
175 struct nhlt_acpi_table
*nhlt
= (struct nhlt_acpi_table
*)skl
->nhlt
;
176 struct nhlt_endpoint
*epnt
;
177 struct nhlt_dmic_array_config
*cfg
;
178 struct device
*dev
= &skl
->pci
->dev
;
179 unsigned int dmic_geo
= 0;
182 epnt
= (struct nhlt_endpoint
*)nhlt
->desc
;
184 for (j
= 0; j
< nhlt
->endpoint_count
; j
++) {
185 if (epnt
->linktype
== NHLT_LINK_DMIC
) {
186 cfg
= (struct nhlt_dmic_array_config
*)
188 switch (cfg
->array_type
) {
189 case NHLT_MIC_ARRAY_2CH_SMALL
:
190 case NHLT_MIC_ARRAY_2CH_BIG
:
191 dmic_geo
|= MIC_ARRAY_2CH
;
194 case NHLT_MIC_ARRAY_4CH_1ST_GEOM
:
195 case NHLT_MIC_ARRAY_4CH_L_SHAPED
:
196 case NHLT_MIC_ARRAY_4CH_2ND_GEOM
:
197 dmic_geo
|= MIC_ARRAY_4CH
;
201 dev_warn(dev
, "undefined DMIC array_type 0x%0x\n",
206 epnt
= (struct nhlt_endpoint
*)((u8
*)epnt
+ epnt
->length
);
212 static void skl_nhlt_trim_space(char *trim
)
219 for (i
= 0; s
[i
]; i
++) {
227 int skl_nhlt_update_topology_bin(struct skl
*skl
)
229 struct nhlt_acpi_table
*nhlt
= (struct nhlt_acpi_table
*)skl
->nhlt
;
230 struct hdac_bus
*bus
= ebus_to_hbus(&skl
->ebus
);
231 struct device
*dev
= bus
->dev
;
233 dev_dbg(dev
, "oem_id %.6s, oem_table_id %8s oem_revision %d\n",
234 nhlt
->header
.oem_id
, nhlt
->header
.oem_table_id
,
235 nhlt
->header
.oem_revision
);
237 snprintf(skl
->tplg_name
, sizeof(skl
->tplg_name
), "%x-%.6s-%.8s-%d%s",
238 skl
->pci_id
, nhlt
->header
.oem_id
, nhlt
->header
.oem_table_id
,
239 nhlt
->header
.oem_revision
, "-tplg.bin");
241 skl_nhlt_trim_space(skl
->tplg_name
);
246 static ssize_t
skl_nhlt_platform_id_show(struct device
*dev
,
247 struct device_attribute
*attr
, char *buf
)
249 struct pci_dev
*pci
= to_pci_dev(dev
);
250 struct hdac_ext_bus
*ebus
= pci_get_drvdata(pci
);
251 struct skl
*skl
= ebus_to_skl(ebus
);
252 struct nhlt_acpi_table
*nhlt
= (struct nhlt_acpi_table
*)skl
->nhlt
;
253 char platform_id
[32];
255 sprintf(platform_id
, "%x-%.6s-%.8s-%d", skl
->pci_id
,
256 nhlt
->header
.oem_id
, nhlt
->header
.oem_table_id
,
257 nhlt
->header
.oem_revision
);
259 skl_nhlt_trim_space(platform_id
);
260 return sprintf(buf
, "%s\n", platform_id
);
263 static DEVICE_ATTR(platform_id
, 0444, skl_nhlt_platform_id_show
, NULL
);
265 int skl_nhlt_create_sysfs(struct skl
*skl
)
267 struct device
*dev
= &skl
->pci
->dev
;
269 if (sysfs_create_file(&dev
->kobj
, &dev_attr_platform_id
.attr
))
270 dev_warn(dev
, "Error creating sysfs entry\n");
275 void skl_nhlt_remove_sysfs(struct skl
*skl
)
277 struct device
*dev
= &skl
->pci
->dev
;
279 sysfs_remove_file(&dev
->kobj
, &dev_attr_platform_id
.attr
);
283 * Queries NHLT for all the fmt configuration for a particular endpoint and
284 * stores all possible rates supported in a rate table for the corresponding
287 static void skl_get_ssp_clks(struct skl
*skl
, struct skl_ssp_clk
*ssp_clks
,
288 struct nhlt_fmt
*fmt
, u8 id
)
290 struct skl_i2s_config_blob_legacy
*i2s_config
;
291 struct skl_clk_parent_src
*parent
;
292 struct skl_ssp_clk
*sclk
, *sclkfs
;
293 struct nhlt_fmt_cfg
*fmt_cfg
;
294 struct wav_fmt_ext
*wav_fmt
;
295 unsigned long rate
= 0;
296 bool present
= false;
303 sclk
= &ssp_clks
[SKL_SCLK_OFS
];
304 sclkfs
= &ssp_clks
[SKL_SCLKFS_OFS
];
306 if (fmt
->fmt_count
== 0)
309 for (i
= 0; i
< fmt
->fmt_count
; i
++) {
310 fmt_cfg
= &fmt
->fmt_config
[i
];
311 wav_fmt
= &fmt_cfg
->fmt_ext
;
313 channels
= wav_fmt
->fmt
.channels
;
314 bps
= wav_fmt
->fmt
.bits_per_sample
;
315 fs
= wav_fmt
->fmt
.samples_per_sec
;
318 * In case of TDM configuration on a ssp, there can
319 * be more than one blob in which channel masks are
320 * different for each usecase for a specific rate and bps.
321 * But the sclk rate will be generated for the total
322 * number of channels used for that endpoint.
324 * So for the given fs and bps, choose blob which has
325 * the superset of all channels for that endpoint and
328 for (j
= i
; j
< fmt
->fmt_count
; j
++) {
329 fmt_cfg
= &fmt
->fmt_config
[j
];
330 wav_fmt
= &fmt_cfg
->fmt_ext
;
331 if ((fs
== wav_fmt
->fmt
.samples_per_sec
) &&
332 (bps
== wav_fmt
->fmt
.bits_per_sample
))
333 channels
= max_t(u16
, channels
,
334 wav_fmt
->fmt
.channels
);
337 rate
= channels
* bps
* fs
;
339 /* check if the rate is added already to the given SSP's sclk */
340 for (j
= 0; (j
< SKL_MAX_CLK_RATES
) &&
341 (sclk
[id
].rate_cfg
[j
].rate
!= 0); j
++) {
342 if (sclk
[id
].rate_cfg
[j
].rate
== rate
) {
348 /* Fill rate and parent for sclk/sclkfs */
350 /* MCLK Divider Source Select */
351 i2s_config
= (struct skl_i2s_config_blob_legacy
*)
352 fmt
->fmt_config
[0].config
.caps
;
353 clk_src
= ((i2s_config
->mclk
.mdivctrl
)
354 & SKL_MNDSS_DIV_CLK_SRC_MASK
) >>
355 SKL_SHIFT(SKL_MNDSS_DIV_CLK_SRC_MASK
);
357 parent
= skl_get_parent_clk(clk_src
);
360 * Do not copy the config data if there is no parent
361 * clock available for this clock source select
366 sclk
[id
].rate_cfg
[rate_index
].rate
= rate
;
367 sclk
[id
].rate_cfg
[rate_index
].config
= fmt_cfg
;
368 sclkfs
[id
].rate_cfg
[rate_index
].rate
= rate
;
369 sclkfs
[id
].rate_cfg
[rate_index
].config
= fmt_cfg
;
370 sclk
[id
].parent_name
= parent
->name
;
371 sclkfs
[id
].parent_name
= parent
->name
;
378 static void skl_get_mclk(struct skl
*skl
, struct skl_ssp_clk
*mclk
,
379 struct nhlt_fmt
*fmt
, u8 id
)
381 struct skl_i2s_config_blob_legacy
*i2s_config
;
382 struct nhlt_specific_cfg
*fmt_cfg
;
383 struct skl_clk_parent_src
*parent
;
384 u32 clkdiv
, div_ratio
;
387 fmt_cfg
= &fmt
->fmt_config
[0].config
;
388 i2s_config
= (struct skl_i2s_config_blob_legacy
*)fmt_cfg
->caps
;
390 /* MCLK Divider Source Select */
391 clk_src
= ((i2s_config
->mclk
.mdivctrl
) & SKL_MCLK_DIV_CLK_SRC_MASK
) >>
392 SKL_SHIFT(SKL_MCLK_DIV_CLK_SRC_MASK
);
394 clkdiv
= i2s_config
->mclk
.mdivr
& SKL_MCLK_DIV_RATIO_MASK
;
399 if (clkdiv
!= SKL_MCLK_DIV_RATIO_MASK
)
400 /* Divider is 2 + clkdiv */
401 div_ratio
= clkdiv
+ 2;
403 /* Calculate MCLK rate from source using div value */
404 parent
= skl_get_parent_clk(clk_src
);
408 mclk
[id
].rate_cfg
[0].rate
= parent
->rate
/div_ratio
;
409 mclk
[id
].rate_cfg
[0].config
= &fmt
->fmt_config
[0];
410 mclk
[id
].parent_name
= parent
->name
;
413 void skl_get_clks(struct skl
*skl
, struct skl_ssp_clk
*ssp_clks
)
415 struct nhlt_acpi_table
*nhlt
= (struct nhlt_acpi_table
*)skl
->nhlt
;
416 struct nhlt_endpoint
*epnt
;
417 struct nhlt_fmt
*fmt
;
421 epnt
= (struct nhlt_endpoint
*)nhlt
->desc
;
422 for (i
= 0; i
< nhlt
->endpoint_count
; i
++) {
423 if (epnt
->linktype
== NHLT_LINK_SSP
) {
424 id
= epnt
->virtual_bus_id
;
426 fmt
= (struct nhlt_fmt
*)(epnt
->config
.caps
427 + epnt
->config
.size
);
429 skl_get_ssp_clks(skl
, ssp_clks
, fmt
, id
);
430 skl_get_mclk(skl
, ssp_clks
, fmt
, id
);
432 epnt
= (struct nhlt_endpoint
*)((u8
*)epnt
+ epnt
->length
);