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