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