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