xref: /OK3568_Linux_fs/kernel/sound/soc/intel/skylake/skl-nhlt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  skl-nhlt.c - Intel SKL Platform NHLT parsing
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Copyright (C) 2015 Intel Corp
6*4882a593Smuzhiyun  *  Author: Sanjiv Kumar <sanjiv.kumar@intel.com>
7*4882a593Smuzhiyun  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun #include <linux/pci.h>
12*4882a593Smuzhiyun #include <sound/intel-nhlt.h>
13*4882a593Smuzhiyun #include "skl.h"
14*4882a593Smuzhiyun #include "skl-i2s.h"
15*4882a593Smuzhiyun 
skl_get_specific_cfg(struct device * dev,struct nhlt_fmt * fmt,u8 no_ch,u32 rate,u16 bps,u8 linktype)16*4882a593Smuzhiyun static struct nhlt_specific_cfg *skl_get_specific_cfg(
17*4882a593Smuzhiyun 		struct device *dev, struct nhlt_fmt *fmt,
18*4882a593Smuzhiyun 		u8 no_ch, u32 rate, u16 bps, u8 linktype)
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun 	struct nhlt_specific_cfg *sp_config;
21*4882a593Smuzhiyun 	struct wav_fmt *wfmt;
22*4882a593Smuzhiyun 	struct nhlt_fmt_cfg *fmt_config = fmt->fmt_config;
23*4882a593Smuzhiyun 	int i;
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun 	dev_dbg(dev, "Format count =%d\n", fmt->fmt_count);
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	for (i = 0; i < fmt->fmt_count; i++) {
28*4882a593Smuzhiyun 		wfmt = &fmt_config->fmt_ext.fmt;
29*4882a593Smuzhiyun 		dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", wfmt->channels,
30*4882a593Smuzhiyun 			 wfmt->bits_per_sample, wfmt->samples_per_sec);
31*4882a593Smuzhiyun 		if (wfmt->channels == no_ch && wfmt->bits_per_sample == bps) {
32*4882a593Smuzhiyun 			/*
33*4882a593Smuzhiyun 			 * if link type is dmic ignore rate check as the blob is
34*4882a593Smuzhiyun 			 * generic for all rates
35*4882a593Smuzhiyun 			 */
36*4882a593Smuzhiyun 			sp_config = &fmt_config->config;
37*4882a593Smuzhiyun 			if (linktype == NHLT_LINK_DMIC)
38*4882a593Smuzhiyun 				return sp_config;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 			if (wfmt->samples_per_sec == rate)
41*4882a593Smuzhiyun 				return sp_config;
42*4882a593Smuzhiyun 		}
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 		fmt_config = (struct nhlt_fmt_cfg *)(fmt_config->config.caps +
45*4882a593Smuzhiyun 						fmt_config->config.size);
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	return NULL;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
dump_config(struct device * dev,u32 instance_id,u8 linktype,u8 s_fmt,u8 num_channels,u32 s_rate,u8 dirn,u16 bps)51*4882a593Smuzhiyun static void dump_config(struct device *dev, u32 instance_id, u8 linktype,
52*4882a593Smuzhiyun 		u8 s_fmt, u8 num_channels, u32 s_rate, u8 dirn, u16 bps)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	dev_dbg(dev, "Input configuration\n");
55*4882a593Smuzhiyun 	dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", num_channels, s_fmt, s_rate);
56*4882a593Smuzhiyun 	dev_dbg(dev, "vbus_id=%d link_type=%d\n", instance_id, linktype);
57*4882a593Smuzhiyun 	dev_dbg(dev, "bits_per_sample=%d\n", bps);
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
skl_check_ep_match(struct device * dev,struct nhlt_endpoint * epnt,u32 instance_id,u8 link_type,u8 dirn,u8 dev_type)60*4882a593Smuzhiyun static bool skl_check_ep_match(struct device *dev, struct nhlt_endpoint *epnt,
61*4882a593Smuzhiyun 		u32 instance_id, u8 link_type, u8 dirn, u8 dev_type)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	dev_dbg(dev, "vbus_id=%d link_type=%d dir=%d dev_type = %d\n",
64*4882a593Smuzhiyun 			epnt->virtual_bus_id, epnt->linktype,
65*4882a593Smuzhiyun 			epnt->direction, epnt->device_type);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	if ((epnt->virtual_bus_id == instance_id) &&
68*4882a593Smuzhiyun 			(epnt->linktype == link_type) &&
69*4882a593Smuzhiyun 			(epnt->direction == dirn)) {
70*4882a593Smuzhiyun 		/* do not check dev_type for DMIC link type */
71*4882a593Smuzhiyun 		if (epnt->linktype == NHLT_LINK_DMIC)
72*4882a593Smuzhiyun 			return true;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 		if (epnt->device_type == dev_type)
75*4882a593Smuzhiyun 			return true;
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	return false;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun struct nhlt_specific_cfg
skl_get_ep_blob(struct skl_dev * skl,u32 instance,u8 link_type,u8 s_fmt,u8 num_ch,u32 s_rate,u8 dirn,u8 dev_type)82*4882a593Smuzhiyun *skl_get_ep_blob(struct skl_dev *skl, u32 instance, u8 link_type,
83*4882a593Smuzhiyun 			u8 s_fmt, u8 num_ch, u32 s_rate,
84*4882a593Smuzhiyun 			u8 dirn, u8 dev_type)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	struct nhlt_fmt *fmt;
87*4882a593Smuzhiyun 	struct nhlt_endpoint *epnt;
88*4882a593Smuzhiyun 	struct hdac_bus *bus = skl_to_bus(skl);
89*4882a593Smuzhiyun 	struct device *dev = bus->dev;
90*4882a593Smuzhiyun 	struct nhlt_specific_cfg *sp_config;
91*4882a593Smuzhiyun 	struct nhlt_acpi_table *nhlt = skl->nhlt;
92*4882a593Smuzhiyun 	u16 bps = (s_fmt == 16) ? 16 : 32;
93*4882a593Smuzhiyun 	u8 j;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	dump_config(dev, instance, link_type, s_fmt, num_ch, s_rate, dirn, bps);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	epnt = (struct nhlt_endpoint *)nhlt->desc;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	dev_dbg(dev, "endpoint count =%d\n", nhlt->endpoint_count);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	for (j = 0; j < nhlt->endpoint_count; j++) {
102*4882a593Smuzhiyun 		if (skl_check_ep_match(dev, epnt, instance, link_type,
103*4882a593Smuzhiyun 						dirn, dev_type)) {
104*4882a593Smuzhiyun 			fmt = (struct nhlt_fmt *)(epnt->config.caps +
105*4882a593Smuzhiyun 						 epnt->config.size);
106*4882a593Smuzhiyun 			sp_config = skl_get_specific_cfg(dev, fmt, num_ch,
107*4882a593Smuzhiyun 							s_rate, bps, link_type);
108*4882a593Smuzhiyun 			if (sp_config)
109*4882a593Smuzhiyun 				return sp_config;
110*4882a593Smuzhiyun 		}
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 		epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	return NULL;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
skl_nhlt_trim_space(char * trim)118*4882a593Smuzhiyun static void skl_nhlt_trim_space(char *trim)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	char *s = trim;
121*4882a593Smuzhiyun 	int cnt;
122*4882a593Smuzhiyun 	int i;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	cnt = 0;
125*4882a593Smuzhiyun 	for (i = 0; s[i]; i++) {
126*4882a593Smuzhiyun 		if (!isspace(s[i]))
127*4882a593Smuzhiyun 			s[cnt++] = s[i];
128*4882a593Smuzhiyun 	}
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	s[cnt] = '\0';
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
skl_nhlt_update_topology_bin(struct skl_dev * skl)133*4882a593Smuzhiyun int skl_nhlt_update_topology_bin(struct skl_dev *skl)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
136*4882a593Smuzhiyun 	struct hdac_bus *bus = skl_to_bus(skl);
137*4882a593Smuzhiyun 	struct device *dev = bus->dev;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	dev_dbg(dev, "oem_id %.6s, oem_table_id %.8s oem_revision %d\n",
140*4882a593Smuzhiyun 		nhlt->header.oem_id, nhlt->header.oem_table_id,
141*4882a593Smuzhiyun 		nhlt->header.oem_revision);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	snprintf(skl->tplg_name, sizeof(skl->tplg_name), "%x-%.6s-%.8s-%d%s",
144*4882a593Smuzhiyun 		skl->pci_id, nhlt->header.oem_id, nhlt->header.oem_table_id,
145*4882a593Smuzhiyun 		nhlt->header.oem_revision, "-tplg.bin");
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	skl_nhlt_trim_space(skl->tplg_name);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	return 0;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
skl_nhlt_platform_id_show(struct device * dev,struct device_attribute * attr,char * buf)152*4882a593Smuzhiyun static ssize_t skl_nhlt_platform_id_show(struct device *dev,
153*4882a593Smuzhiyun 			struct device_attribute *attr, char *buf)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	struct pci_dev *pci = to_pci_dev(dev);
156*4882a593Smuzhiyun 	struct hdac_bus *bus = pci_get_drvdata(pci);
157*4882a593Smuzhiyun 	struct skl_dev *skl = bus_to_skl(bus);
158*4882a593Smuzhiyun 	struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
159*4882a593Smuzhiyun 	char platform_id[32];
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	sprintf(platform_id, "%x-%.6s-%.8s-%d", skl->pci_id,
162*4882a593Smuzhiyun 			nhlt->header.oem_id, nhlt->header.oem_table_id,
163*4882a593Smuzhiyun 			nhlt->header.oem_revision);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	skl_nhlt_trim_space(platform_id);
166*4882a593Smuzhiyun 	return sprintf(buf, "%s\n", platform_id);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun static DEVICE_ATTR(platform_id, 0444, skl_nhlt_platform_id_show, NULL);
170*4882a593Smuzhiyun 
skl_nhlt_create_sysfs(struct skl_dev * skl)171*4882a593Smuzhiyun int skl_nhlt_create_sysfs(struct skl_dev *skl)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun 	struct device *dev = &skl->pci->dev;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (sysfs_create_file(&dev->kobj, &dev_attr_platform_id.attr))
176*4882a593Smuzhiyun 		dev_warn(dev, "Error creating sysfs entry\n");
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	return 0;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
skl_nhlt_remove_sysfs(struct skl_dev * skl)181*4882a593Smuzhiyun void skl_nhlt_remove_sysfs(struct skl_dev *skl)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	struct device *dev = &skl->pci->dev;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (skl->nhlt)
186*4882a593Smuzhiyun 		sysfs_remove_file(&dev->kobj, &dev_attr_platform_id.attr);
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /*
190*4882a593Smuzhiyun  * Queries NHLT for all the fmt configuration for a particular endpoint and
191*4882a593Smuzhiyun  * stores all possible rates supported in a rate table for the corresponding
192*4882a593Smuzhiyun  * sclk/sclkfs.
193*4882a593Smuzhiyun  */
skl_get_ssp_clks(struct skl_dev * skl,struct skl_ssp_clk * ssp_clks,struct nhlt_fmt * fmt,u8 id)194*4882a593Smuzhiyun static void skl_get_ssp_clks(struct skl_dev *skl, struct skl_ssp_clk *ssp_clks,
195*4882a593Smuzhiyun 				struct nhlt_fmt *fmt, u8 id)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	struct skl_i2s_config_blob_ext *i2s_config_ext;
198*4882a593Smuzhiyun 	struct skl_i2s_config_blob_legacy *i2s_config;
199*4882a593Smuzhiyun 	struct skl_clk_parent_src *parent;
200*4882a593Smuzhiyun 	struct skl_ssp_clk *sclk, *sclkfs;
201*4882a593Smuzhiyun 	struct nhlt_fmt_cfg *fmt_cfg;
202*4882a593Smuzhiyun 	struct wav_fmt_ext *wav_fmt;
203*4882a593Smuzhiyun 	unsigned long rate;
204*4882a593Smuzhiyun 	int rate_index = 0;
205*4882a593Smuzhiyun 	u16 channels, bps;
206*4882a593Smuzhiyun 	u8 clk_src;
207*4882a593Smuzhiyun 	int i, j;
208*4882a593Smuzhiyun 	u32 fs;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	sclk = &ssp_clks[SKL_SCLK_OFS];
211*4882a593Smuzhiyun 	sclkfs = &ssp_clks[SKL_SCLKFS_OFS];
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (fmt->fmt_count == 0)
214*4882a593Smuzhiyun 		return;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	fmt_cfg = (struct nhlt_fmt_cfg *)fmt->fmt_config;
217*4882a593Smuzhiyun 	for (i = 0; i < fmt->fmt_count; i++) {
218*4882a593Smuzhiyun 		struct nhlt_fmt_cfg *saved_fmt_cfg = fmt_cfg;
219*4882a593Smuzhiyun 		bool present = false;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 		wav_fmt = &saved_fmt_cfg->fmt_ext;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 		channels = wav_fmt->fmt.channels;
224*4882a593Smuzhiyun 		bps = wav_fmt->fmt.bits_per_sample;
225*4882a593Smuzhiyun 		fs = wav_fmt->fmt.samples_per_sec;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 		/*
228*4882a593Smuzhiyun 		 * In case of TDM configuration on a ssp, there can
229*4882a593Smuzhiyun 		 * be more than one blob in which channel masks are
230*4882a593Smuzhiyun 		 * different for each usecase for a specific rate and bps.
231*4882a593Smuzhiyun 		 * But the sclk rate will be generated for the total
232*4882a593Smuzhiyun 		 * number of channels used for that endpoint.
233*4882a593Smuzhiyun 		 *
234*4882a593Smuzhiyun 		 * So for the given fs and bps, choose blob which has
235*4882a593Smuzhiyun 		 * the superset of all channels for that endpoint and
236*4882a593Smuzhiyun 		 * derive the rate.
237*4882a593Smuzhiyun 		 */
238*4882a593Smuzhiyun 		for (j = i; j < fmt->fmt_count; j++) {
239*4882a593Smuzhiyun 			struct nhlt_fmt_cfg *tmp_fmt_cfg = fmt_cfg;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 			wav_fmt = &tmp_fmt_cfg->fmt_ext;
242*4882a593Smuzhiyun 			if ((fs == wav_fmt->fmt.samples_per_sec) &&
243*4882a593Smuzhiyun 			   (bps == wav_fmt->fmt.bits_per_sample)) {
244*4882a593Smuzhiyun 				channels = max_t(u16, channels,
245*4882a593Smuzhiyun 						wav_fmt->fmt.channels);
246*4882a593Smuzhiyun 				saved_fmt_cfg = tmp_fmt_cfg;
247*4882a593Smuzhiyun 			}
248*4882a593Smuzhiyun 			/* Move to the next nhlt_fmt_cfg */
249*4882a593Smuzhiyun 			tmp_fmt_cfg = (struct nhlt_fmt_cfg *)(tmp_fmt_cfg->config.caps +
250*4882a593Smuzhiyun 							      tmp_fmt_cfg->config.size);
251*4882a593Smuzhiyun 		}
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 		rate = channels * bps * fs;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 		/* check if the rate is added already to the given SSP's sclk */
256*4882a593Smuzhiyun 		for (j = 0; (j < SKL_MAX_CLK_RATES) &&
257*4882a593Smuzhiyun 			    (sclk[id].rate_cfg[j].rate != 0); j++) {
258*4882a593Smuzhiyun 			if (sclk[id].rate_cfg[j].rate == rate) {
259*4882a593Smuzhiyun 				present = true;
260*4882a593Smuzhiyun 				break;
261*4882a593Smuzhiyun 			}
262*4882a593Smuzhiyun 		}
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 		/* Fill rate and parent for sclk/sclkfs */
265*4882a593Smuzhiyun 		if (!present) {
266*4882a593Smuzhiyun 			struct nhlt_fmt_cfg *first_fmt_cfg;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 			first_fmt_cfg = (struct nhlt_fmt_cfg *)fmt->fmt_config;
269*4882a593Smuzhiyun 			i2s_config_ext = (struct skl_i2s_config_blob_ext *)
270*4882a593Smuzhiyun 						first_fmt_cfg->config.caps;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 			/* MCLK Divider Source Select */
273*4882a593Smuzhiyun 			if (is_legacy_blob(i2s_config_ext->hdr.sig)) {
274*4882a593Smuzhiyun 				i2s_config = ext_to_legacy_blob(i2s_config_ext);
275*4882a593Smuzhiyun 				clk_src = get_clk_src(i2s_config->mclk,
276*4882a593Smuzhiyun 						SKL_MNDSS_DIV_CLK_SRC_MASK);
277*4882a593Smuzhiyun 			} else {
278*4882a593Smuzhiyun 				clk_src = get_clk_src(i2s_config_ext->mclk,
279*4882a593Smuzhiyun 						SKL_MNDSS_DIV_CLK_SRC_MASK);
280*4882a593Smuzhiyun 			}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 			parent = skl_get_parent_clk(clk_src);
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 			/* Move to the next nhlt_fmt_cfg */
285*4882a593Smuzhiyun 			fmt_cfg = (struct nhlt_fmt_cfg *)(fmt_cfg->config.caps +
286*4882a593Smuzhiyun 							  fmt_cfg->config.size);
287*4882a593Smuzhiyun 			/*
288*4882a593Smuzhiyun 			 * Do not copy the config data if there is no parent
289*4882a593Smuzhiyun 			 * clock available for this clock source select
290*4882a593Smuzhiyun 			 */
291*4882a593Smuzhiyun 			if (!parent)
292*4882a593Smuzhiyun 				continue;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 			sclk[id].rate_cfg[rate_index].rate = rate;
295*4882a593Smuzhiyun 			sclk[id].rate_cfg[rate_index].config = saved_fmt_cfg;
296*4882a593Smuzhiyun 			sclkfs[id].rate_cfg[rate_index].rate = rate;
297*4882a593Smuzhiyun 			sclkfs[id].rate_cfg[rate_index].config = saved_fmt_cfg;
298*4882a593Smuzhiyun 			sclk[id].parent_name = parent->name;
299*4882a593Smuzhiyun 			sclkfs[id].parent_name = parent->name;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 			rate_index++;
302*4882a593Smuzhiyun 		}
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
skl_get_mclk(struct skl_dev * skl,struct skl_ssp_clk * mclk,struct nhlt_fmt * fmt,u8 id)306*4882a593Smuzhiyun static void skl_get_mclk(struct skl_dev *skl, struct skl_ssp_clk *mclk,
307*4882a593Smuzhiyun 				struct nhlt_fmt *fmt, u8 id)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	struct skl_i2s_config_blob_ext *i2s_config_ext;
310*4882a593Smuzhiyun 	struct skl_i2s_config_blob_legacy *i2s_config;
311*4882a593Smuzhiyun 	struct nhlt_fmt_cfg *fmt_cfg;
312*4882a593Smuzhiyun 	struct skl_clk_parent_src *parent;
313*4882a593Smuzhiyun 	u32 clkdiv, div_ratio;
314*4882a593Smuzhiyun 	u8 clk_src;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	fmt_cfg = (struct nhlt_fmt_cfg *)fmt->fmt_config;
317*4882a593Smuzhiyun 	i2s_config_ext = (struct skl_i2s_config_blob_ext *)fmt_cfg->config.caps;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	/* MCLK Divider Source Select and divider */
320*4882a593Smuzhiyun 	if (is_legacy_blob(i2s_config_ext->hdr.sig)) {
321*4882a593Smuzhiyun 		i2s_config = ext_to_legacy_blob(i2s_config_ext);
322*4882a593Smuzhiyun 		clk_src = get_clk_src(i2s_config->mclk,
323*4882a593Smuzhiyun 				SKL_MCLK_DIV_CLK_SRC_MASK);
324*4882a593Smuzhiyun 		clkdiv = i2s_config->mclk.mdivr &
325*4882a593Smuzhiyun 				SKL_MCLK_DIV_RATIO_MASK;
326*4882a593Smuzhiyun 	} else {
327*4882a593Smuzhiyun 		clk_src = get_clk_src(i2s_config_ext->mclk,
328*4882a593Smuzhiyun 				SKL_MCLK_DIV_CLK_SRC_MASK);
329*4882a593Smuzhiyun 		clkdiv = i2s_config_ext->mclk.mdivr[0] &
330*4882a593Smuzhiyun 				SKL_MCLK_DIV_RATIO_MASK;
331*4882a593Smuzhiyun 	}
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	/* bypass divider */
334*4882a593Smuzhiyun 	div_ratio = 1;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	if (clkdiv != SKL_MCLK_DIV_RATIO_MASK)
337*4882a593Smuzhiyun 		/* Divider is 2 + clkdiv */
338*4882a593Smuzhiyun 		div_ratio = clkdiv + 2;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	/* Calculate MCLK rate from source using div value */
341*4882a593Smuzhiyun 	parent = skl_get_parent_clk(clk_src);
342*4882a593Smuzhiyun 	if (!parent)
343*4882a593Smuzhiyun 		return;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	mclk[id].rate_cfg[0].rate = parent->rate/div_ratio;
346*4882a593Smuzhiyun 	mclk[id].rate_cfg[0].config = fmt_cfg;
347*4882a593Smuzhiyun 	mclk[id].parent_name = parent->name;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun 
skl_get_clks(struct skl_dev * skl,struct skl_ssp_clk * ssp_clks)350*4882a593Smuzhiyun void skl_get_clks(struct skl_dev *skl, struct skl_ssp_clk *ssp_clks)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun 	struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
353*4882a593Smuzhiyun 	struct nhlt_endpoint *epnt;
354*4882a593Smuzhiyun 	struct nhlt_fmt *fmt;
355*4882a593Smuzhiyun 	int i;
356*4882a593Smuzhiyun 	u8 id;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	epnt = (struct nhlt_endpoint *)nhlt->desc;
359*4882a593Smuzhiyun 	for (i = 0; i < nhlt->endpoint_count; i++) {
360*4882a593Smuzhiyun 		if (epnt->linktype == NHLT_LINK_SSP) {
361*4882a593Smuzhiyun 			id = epnt->virtual_bus_id;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 			fmt = (struct nhlt_fmt *)(epnt->config.caps
364*4882a593Smuzhiyun 					+ epnt->config.size);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 			skl_get_ssp_clks(skl, ssp_clks, fmt, id);
367*4882a593Smuzhiyun 			skl_get_mclk(skl, ssp_clks, fmt, id);
368*4882a593Smuzhiyun 		}
369*4882a593Smuzhiyun 		epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun }
372