GNU Linux-libre 4.19.286-gnu1
[releases.git] / sound / soc / intel / skylake / skl-nhlt.c
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), "/*(DEBLOBBED)*/");
239
240         skl_nhlt_trim_space(skl->tplg_name);
241
242         return 0;
243 }
244
245 static ssize_t skl_nhlt_platform_id_show(struct device *dev,
246                         struct device_attribute *attr, char *buf)
247 {
248         struct pci_dev *pci = to_pci_dev(dev);
249         struct hdac_bus *bus = pci_get_drvdata(pci);
250         struct skl *skl = bus_to_skl(bus);
251         struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
252         char platform_id[32];
253
254         sprintf(platform_id, "%x-%.6s-%.8s-%d", skl->pci_id,
255                         nhlt->header.oem_id, nhlt->header.oem_table_id,
256                         nhlt->header.oem_revision);
257
258         skl_nhlt_trim_space(platform_id);
259         return sprintf(buf, "%s\n", platform_id);
260 }
261
262 static DEVICE_ATTR(platform_id, 0444, skl_nhlt_platform_id_show, NULL);
263
264 int skl_nhlt_create_sysfs(struct skl *skl)
265 {
266         struct device *dev = &skl->pci->dev;
267
268         if (sysfs_create_file(&dev->kobj, &dev_attr_platform_id.attr))
269                 dev_warn(dev, "Error creating sysfs entry\n");
270
271         return 0;
272 }
273
274 void skl_nhlt_remove_sysfs(struct skl *skl)
275 {
276         struct device *dev = &skl->pci->dev;
277
278         sysfs_remove_file(&dev->kobj, &dev_attr_platform_id.attr);
279 }
280
281 /*
282  * Queries NHLT for all the fmt configuration for a particular endpoint and
283  * stores all possible rates supported in a rate table for the corresponding
284  * sclk/sclkfs.
285  */
286 static void skl_get_ssp_clks(struct skl *skl, struct skl_ssp_clk *ssp_clks,
287                                 struct nhlt_fmt *fmt, u8 id)
288 {
289         struct skl_i2s_config_blob_ext *i2s_config_ext;
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;
297         int rate_index = 0;
298         u16 channels, bps;
299         u8 clk_src;
300         int i, j;
301         u32 fs;
302
303         sclk = &ssp_clks[SKL_SCLK_OFS];
304         sclkfs = &ssp_clks[SKL_SCLKFS_OFS];
305
306         if (fmt->fmt_count == 0)
307                 return;
308
309         for (i = 0; i < fmt->fmt_count; i++) {
310                 fmt_cfg = &fmt->fmt_config[i];
311                 wav_fmt = &fmt_cfg->fmt_ext;
312
313                 channels = wav_fmt->fmt.channels;
314                 bps = wav_fmt->fmt.bits_per_sample;
315                 fs = wav_fmt->fmt.samples_per_sec;
316
317                 /*
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.
323                  *
324                  * So for the given fs and bps, choose blob which has
325                  * the superset of all channels for that endpoint and
326                  * derive the rate.
327                  */
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);
335                 }
336
337                 rate = channels * bps * fs;
338
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) {
343                                 present = true;
344                                 break;
345                         }
346                 }
347
348                 /* Fill rate and parent for sclk/sclkfs */
349                 if (!present) {
350                         i2s_config_ext = (struct skl_i2s_config_blob_ext *)
351                                                 fmt->fmt_config[0].config.caps;
352
353                         /* MCLK Divider Source Select */
354                         if (is_legacy_blob(i2s_config_ext->hdr.sig)) {
355                                 i2s_config = ext_to_legacy_blob(i2s_config_ext);
356                                 clk_src = get_clk_src(i2s_config->mclk,
357                                                 SKL_MNDSS_DIV_CLK_SRC_MASK);
358                         } else {
359                                 clk_src = get_clk_src(i2s_config_ext->mclk,
360                                                 SKL_MNDSS_DIV_CLK_SRC_MASK);
361                         }
362
363                         parent = skl_get_parent_clk(clk_src);
364
365                         /*
366                          * Do not copy the config data if there is no parent
367                          * clock available for this clock source select
368                          */
369                         if (!parent)
370                                 continue;
371
372                         sclk[id].rate_cfg[rate_index].rate = rate;
373                         sclk[id].rate_cfg[rate_index].config = fmt_cfg;
374                         sclkfs[id].rate_cfg[rate_index].rate = rate;
375                         sclkfs[id].rate_cfg[rate_index].config = fmt_cfg;
376                         sclk[id].parent_name = parent->name;
377                         sclkfs[id].parent_name = parent->name;
378
379                         rate_index++;
380                 }
381         }
382 }
383
384 static void skl_get_mclk(struct skl *skl, struct skl_ssp_clk *mclk,
385                                 struct nhlt_fmt *fmt, u8 id)
386 {
387         struct skl_i2s_config_blob_ext *i2s_config_ext;
388         struct skl_i2s_config_blob_legacy *i2s_config;
389         struct nhlt_specific_cfg *fmt_cfg;
390         struct skl_clk_parent_src *parent;
391         u32 clkdiv, div_ratio;
392         u8 clk_src;
393
394         fmt_cfg = &fmt->fmt_config[0].config;
395         i2s_config_ext = (struct skl_i2s_config_blob_ext *)fmt_cfg->caps;
396
397         /* MCLK Divider Source Select and divider */
398         if (is_legacy_blob(i2s_config_ext->hdr.sig)) {
399                 i2s_config = ext_to_legacy_blob(i2s_config_ext);
400                 clk_src = get_clk_src(i2s_config->mclk,
401                                 SKL_MCLK_DIV_CLK_SRC_MASK);
402                 clkdiv = i2s_config->mclk.mdivr &
403                                 SKL_MCLK_DIV_RATIO_MASK;
404         } else {
405                 clk_src = get_clk_src(i2s_config_ext->mclk,
406                                 SKL_MCLK_DIV_CLK_SRC_MASK);
407                 clkdiv = i2s_config_ext->mclk.mdivr[0] &
408                                 SKL_MCLK_DIV_RATIO_MASK;
409         }
410
411         /* bypass divider */
412         div_ratio = 1;
413
414         if (clkdiv != SKL_MCLK_DIV_RATIO_MASK)
415                 /* Divider is 2 + clkdiv */
416                 div_ratio = clkdiv + 2;
417
418         /* Calculate MCLK rate from source using div value */
419         parent = skl_get_parent_clk(clk_src);
420         if (!parent)
421                 return;
422
423         mclk[id].rate_cfg[0].rate = parent->rate/div_ratio;
424         mclk[id].rate_cfg[0].config = &fmt->fmt_config[0];
425         mclk[id].parent_name = parent->name;
426 }
427
428 void skl_get_clks(struct skl *skl, struct skl_ssp_clk *ssp_clks)
429 {
430         struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
431         struct nhlt_endpoint *epnt;
432         struct nhlt_fmt *fmt;
433         int i;
434         u8 id;
435
436         epnt = (struct nhlt_endpoint *)nhlt->desc;
437         for (i = 0; i < nhlt->endpoint_count; i++) {
438                 if (epnt->linktype == NHLT_LINK_SSP) {
439                         id = epnt->virtual_bus_id;
440
441                         fmt = (struct nhlt_fmt *)(epnt->config.caps
442                                         + epnt->config.size);
443
444                         skl_get_ssp_clks(skl, ssp_clks, fmt, id);
445                         skl_get_mclk(skl, ssp_clks, fmt, id);
446                 }
447                 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
448         }
449 }