ASoC: acpi: fix machine driver selection based on quirk
[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
23 #define NHLT_ACPI_HEADER_SIG    "NHLT"
24
25 /* Unique identification for getting NHLT blobs */
26 static guid_t osc_guid =
27         GUID_INIT(0xA69F886E, 0x6CEB, 0x4594,
28                   0xA4, 0x1F, 0x7B, 0x5D, 0xCE, 0x24, 0xC5, 0x53);
29
30 struct nhlt_acpi_table *skl_nhlt_init(struct device *dev)
31 {
32         acpi_handle handle;
33         union acpi_object *obj;
34         struct nhlt_resource_desc  *nhlt_ptr = NULL;
35         struct nhlt_acpi_table *nhlt_table = NULL;
36
37         handle = ACPI_HANDLE(dev);
38         if (!handle) {
39                 dev_err(dev, "Didn't find ACPI_HANDLE\n");
40                 return NULL;
41         }
42
43         obj = acpi_evaluate_dsm(handle, &osc_guid, 1, 1, NULL);
44         if (obj && obj->type == ACPI_TYPE_BUFFER) {
45                 nhlt_ptr = (struct nhlt_resource_desc  *)obj->buffer.pointer;
46                 if (nhlt_ptr->length)
47                         nhlt_table = (struct nhlt_acpi_table *)
48                                 memremap(nhlt_ptr->min_addr, nhlt_ptr->length,
49                                 MEMREMAP_WB);
50                 ACPI_FREE(obj);
51                 if (nhlt_table && (strncmp(nhlt_table->header.signature,
52                                         NHLT_ACPI_HEADER_SIG,
53                                         strlen(NHLT_ACPI_HEADER_SIG)) != 0)) {
54                         memunmap(nhlt_table);
55                         dev_err(dev, "NHLT ACPI header signature incorrect\n");
56                         return NULL;
57                 }
58                 return nhlt_table;
59         }
60
61         dev_err(dev, "device specific method to extract NHLT blob failed\n");
62         return NULL;
63 }
64
65 void skl_nhlt_free(struct nhlt_acpi_table *nhlt)
66 {
67         memunmap((void *) nhlt);
68 }
69
70 static struct nhlt_specific_cfg *skl_get_specific_cfg(
71                 struct device *dev, struct nhlt_fmt *fmt,
72                 u8 no_ch, u32 rate, u16 bps, u8 linktype)
73 {
74         struct nhlt_specific_cfg *sp_config;
75         struct wav_fmt *wfmt;
76         struct nhlt_fmt_cfg *fmt_config = fmt->fmt_config;
77         int i;
78
79         dev_dbg(dev, "Format count =%d\n", fmt->fmt_count);
80
81         for (i = 0; i < fmt->fmt_count; i++) {
82                 wfmt = &fmt_config->fmt_ext.fmt;
83                 dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", wfmt->channels,
84                          wfmt->bits_per_sample, wfmt->samples_per_sec);
85                 if (wfmt->channels == no_ch && wfmt->bits_per_sample == bps) {
86                         /*
87                          * if link type is dmic ignore rate check as the blob is
88                          * generic for all rates
89                          */
90                         sp_config = &fmt_config->config;
91                         if (linktype == NHLT_LINK_DMIC)
92                                 return sp_config;
93
94                         if (wfmt->samples_per_sec == rate)
95                                 return sp_config;
96                 }
97
98                 fmt_config = (struct nhlt_fmt_cfg *)(fmt_config->config.caps +
99                                                 fmt_config->config.size);
100         }
101
102         return NULL;
103 }
104
105 static void dump_config(struct device *dev, u32 instance_id, u8 linktype,
106                 u8 s_fmt, u8 num_channels, u32 s_rate, u8 dirn, u16 bps)
107 {
108         dev_dbg(dev, "Input configuration\n");
109         dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", num_channels, s_fmt, s_rate);
110         dev_dbg(dev, "vbus_id=%d link_type=%d\n", instance_id, linktype);
111         dev_dbg(dev, "bits_per_sample=%d\n", bps);
112 }
113
114 static bool skl_check_ep_match(struct device *dev, struct nhlt_endpoint *epnt,
115                 u32 instance_id, u8 link_type, u8 dirn, u8 dev_type)
116 {
117         dev_dbg(dev, "vbus_id=%d link_type=%d dir=%d dev_type = %d\n",
118                         epnt->virtual_bus_id, epnt->linktype,
119                         epnt->direction, epnt->device_type);
120
121         if ((epnt->virtual_bus_id == instance_id) &&
122                         (epnt->linktype == link_type) &&
123                         (epnt->direction == dirn) &&
124                         (epnt->device_type == dev_type))
125                 return true;
126         else
127                 return false;
128 }
129
130 struct nhlt_specific_cfg
131 *skl_get_ep_blob(struct skl *skl, u32 instance, u8 link_type,
132                         u8 s_fmt, u8 num_ch, u32 s_rate,
133                         u8 dirn, u8 dev_type)
134 {
135         struct nhlt_fmt *fmt;
136         struct nhlt_endpoint *epnt;
137         struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
138         struct device *dev = bus->dev;
139         struct nhlt_specific_cfg *sp_config;
140         struct nhlt_acpi_table *nhlt = skl->nhlt;
141         u16 bps = (s_fmt == 16) ? 16 : 32;
142         u8 j;
143
144         dump_config(dev, instance, link_type, s_fmt, num_ch, s_rate, dirn, bps);
145
146         epnt = (struct nhlt_endpoint *)nhlt->desc;
147
148         dev_dbg(dev, "endpoint count =%d\n", nhlt->endpoint_count);
149
150         for (j = 0; j < nhlt->endpoint_count; j++) {
151                 if (skl_check_ep_match(dev, epnt, instance, link_type,
152                                                 dirn, dev_type)) {
153                         fmt = (struct nhlt_fmt *)(epnt->config.caps +
154                                                  epnt->config.size);
155                         sp_config = skl_get_specific_cfg(dev, fmt, num_ch,
156                                                         s_rate, bps, link_type);
157                         if (sp_config)
158                                 return sp_config;
159                 }
160
161                 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
162         }
163
164         return NULL;
165 }
166
167 int skl_get_dmic_geo(struct skl *skl)
168 {
169         struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
170         struct nhlt_endpoint *epnt;
171         struct nhlt_dmic_array_config *cfg;
172         struct device *dev = &skl->pci->dev;
173         unsigned int dmic_geo = 0;
174         u8 j;
175
176         epnt = (struct nhlt_endpoint *)nhlt->desc;
177
178         for (j = 0; j < nhlt->endpoint_count; j++) {
179                 if (epnt->linktype == NHLT_LINK_DMIC) {
180                         cfg = (struct nhlt_dmic_array_config  *)
181                                         (epnt->config.caps);
182                         switch (cfg->array_type) {
183                         case NHLT_MIC_ARRAY_2CH_SMALL:
184                         case NHLT_MIC_ARRAY_2CH_BIG:
185                                 dmic_geo |= MIC_ARRAY_2CH;
186                                 break;
187
188                         case NHLT_MIC_ARRAY_4CH_1ST_GEOM:
189                         case NHLT_MIC_ARRAY_4CH_L_SHAPED:
190                         case NHLT_MIC_ARRAY_4CH_2ND_GEOM:
191                                 dmic_geo |= MIC_ARRAY_4CH;
192                                 break;
193
194                         default:
195                                 dev_warn(dev, "undefined DMIC array_type 0x%0x\n",
196                                                 cfg->array_type);
197
198                         }
199                 }
200                 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
201         }
202
203         return dmic_geo;
204 }
205
206 static void skl_nhlt_trim_space(char *trim)
207 {
208         char *s = trim;
209         int cnt;
210         int i;
211
212         cnt = 0;
213         for (i = 0; s[i]; i++) {
214                 if (!isspace(s[i]))
215                         s[cnt++] = s[i];
216         }
217
218         s[cnt] = '\0';
219 }
220
221 int skl_nhlt_update_topology_bin(struct skl *skl)
222 {
223         struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
224         struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
225         struct device *dev = bus->dev;
226
227         dev_dbg(dev, "oem_id %.6s, oem_table_id %8s oem_revision %d\n",
228                 nhlt->header.oem_id, nhlt->header.oem_table_id,
229                 nhlt->header.oem_revision);
230
231         snprintf(skl->tplg_name, sizeof(skl->tplg_name), "%x-%.6s-%.8s-%d%s",
232                 skl->pci_id, nhlt->header.oem_id, nhlt->header.oem_table_id,
233                 nhlt->header.oem_revision, "-tplg.bin");
234
235         skl_nhlt_trim_space(skl->tplg_name);
236
237         return 0;
238 }
239
240 static ssize_t skl_nhlt_platform_id_show(struct device *dev,
241                         struct device_attribute *attr, char *buf)
242 {
243         struct pci_dev *pci = to_pci_dev(dev);
244         struct hdac_ext_bus *ebus = pci_get_drvdata(pci);
245         struct skl *skl = ebus_to_skl(ebus);
246         struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
247         char platform_id[32];
248
249         sprintf(platform_id, "%x-%.6s-%.8s-%d", skl->pci_id,
250                         nhlt->header.oem_id, nhlt->header.oem_table_id,
251                         nhlt->header.oem_revision);
252
253         skl_nhlt_trim_space(platform_id);
254         return sprintf(buf, "%s\n", platform_id);
255 }
256
257 static DEVICE_ATTR(platform_id, 0444, skl_nhlt_platform_id_show, NULL);
258
259 int skl_nhlt_create_sysfs(struct skl *skl)
260 {
261         struct device *dev = &skl->pci->dev;
262
263         if (sysfs_create_file(&dev->kobj, &dev_attr_platform_id.attr))
264                 dev_warn(dev, "Error creating sysfs entry\n");
265
266         return 0;
267 }
268
269 void skl_nhlt_remove_sysfs(struct skl *skl)
270 {
271         struct device *dev = &skl->pci->dev;
272
273         sysfs_remove_file(&dev->kobj, &dev_attr_platform_id.attr);
274 }