Merge remote-tracking branch 'asoc/fix/intel' into asoc-linus
[muen/linux.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 /* Unique identification for getting NHLT blobs */
24 static u8 OSC_UUID[16] = {0x6E, 0x88, 0x9F, 0xA6, 0xEB, 0x6C, 0x94, 0x45,
25                                 0xA4, 0x1F, 0x7B, 0x5D, 0xCE, 0x24, 0xC5, 0x53};
26
27 #define DSDT_NHLT_PATH "\\_SB.PCI0.HDAS"
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         if (ACPI_FAILURE(acpi_get_handle(NULL, DSDT_NHLT_PATH, &handle))) {
37                 dev_err(dev, "Requested NHLT device not found\n");
38                 return NULL;
39         }
40
41         obj = acpi_evaluate_dsm(handle, OSC_UUID, 1, 1, NULL);
42         if (obj && obj->type == ACPI_TYPE_BUFFER) {
43                 nhlt_ptr = (struct nhlt_resource_desc  *)obj->buffer.pointer;
44                 nhlt_table = (struct nhlt_acpi_table *)
45                                 memremap(nhlt_ptr->min_addr, nhlt_ptr->length,
46                                 MEMREMAP_WB);
47                 ACPI_FREE(obj);
48                 return nhlt_table;
49         }
50
51         dev_err(dev, "device specific method to extract NHLT blob failed\n");
52         return NULL;
53 }
54
55 void skl_nhlt_free(struct nhlt_acpi_table *nhlt)
56 {
57         memunmap((void *) nhlt);
58 }
59
60 static struct nhlt_specific_cfg *skl_get_specific_cfg(
61                 struct device *dev, struct nhlt_fmt *fmt,
62                 u8 no_ch, u32 rate, u16 bps, u8 linktype)
63 {
64         struct nhlt_specific_cfg *sp_config;
65         struct wav_fmt *wfmt;
66         struct nhlt_fmt_cfg *fmt_config = fmt->fmt_config;
67         int i;
68
69         dev_dbg(dev, "Format count =%d\n", fmt->fmt_count);
70
71         for (i = 0; i < fmt->fmt_count; i++) {
72                 wfmt = &fmt_config->fmt_ext.fmt;
73                 dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", wfmt->channels,
74                          wfmt->bits_per_sample, wfmt->samples_per_sec);
75                 if (wfmt->channels == no_ch && wfmt->bits_per_sample == bps) {
76                         /*
77                          * if link type is dmic ignore rate check as the blob is
78                          * generic for all rates
79                          */
80                         sp_config = &fmt_config->config;
81                         if (linktype == NHLT_LINK_DMIC)
82                                 return sp_config;
83
84                         if (wfmt->samples_per_sec == rate)
85                                 return sp_config;
86                 }
87
88                 fmt_config = (struct nhlt_fmt_cfg *)(fmt_config->config.caps +
89                                                 fmt_config->config.size);
90         }
91
92         return NULL;
93 }
94
95 static void dump_config(struct device *dev, u32 instance_id, u8 linktype,
96                 u8 s_fmt, u8 num_channels, u32 s_rate, u8 dirn, u16 bps)
97 {
98         dev_dbg(dev, "Input configuration\n");
99         dev_dbg(dev, "ch=%d fmt=%d s_rate=%d\n", num_channels, s_fmt, s_rate);
100         dev_dbg(dev, "vbus_id=%d link_type=%d\n", instance_id, linktype);
101         dev_dbg(dev, "bits_per_sample=%d\n", bps);
102 }
103
104 static bool skl_check_ep_match(struct device *dev, struct nhlt_endpoint *epnt,
105                                 u32 instance_id, u8 link_type, u8 dirn)
106 {
107         dev_dbg(dev, "vbus_id=%d link_type=%d dir=%d\n",
108                 epnt->virtual_bus_id, epnt->linktype, epnt->direction);
109
110         if ((epnt->virtual_bus_id == instance_id) &&
111                         (epnt->linktype == link_type) &&
112                         (epnt->direction == dirn))
113                 return true;
114         else
115                 return false;
116 }
117
118 struct nhlt_specific_cfg
119 *skl_get_ep_blob(struct skl *skl, u32 instance, u8 link_type,
120                         u8 s_fmt, u8 num_ch, u32 s_rate, u8 dirn)
121 {
122         struct nhlt_fmt *fmt;
123         struct nhlt_endpoint *epnt;
124         struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
125         struct device *dev = bus->dev;
126         struct nhlt_specific_cfg *sp_config;
127         struct nhlt_acpi_table *nhlt = skl->nhlt;
128         u16 bps = (s_fmt == 16) ? 16 : 32;
129         u8 j;
130
131         dump_config(dev, instance, link_type, s_fmt, num_ch, s_rate, dirn, bps);
132
133         epnt = (struct nhlt_endpoint *)nhlt->desc;
134
135         dev_dbg(dev, "endpoint count =%d\n", nhlt->endpoint_count);
136
137         for (j = 0; j < nhlt->endpoint_count; j++) {
138                 if (skl_check_ep_match(dev, epnt, instance, link_type, dirn)) {
139                         fmt = (struct nhlt_fmt *)(epnt->config.caps +
140                                                  epnt->config.size);
141                         sp_config = skl_get_specific_cfg(dev, fmt, num_ch,
142                                                         s_rate, bps, link_type);
143                         if (sp_config)
144                                 return sp_config;
145                 }
146
147                 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
148         }
149
150         return NULL;
151 }
152
153 int skl_get_dmic_geo(struct skl *skl)
154 {
155         struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
156         struct nhlt_endpoint *epnt;
157         struct nhlt_dmic_array_config *cfg;
158         struct device *dev = &skl->pci->dev;
159         unsigned int dmic_geo = 0;
160         u8 j;
161
162         epnt = (struct nhlt_endpoint *)nhlt->desc;
163
164         for (j = 0; j < nhlt->endpoint_count; j++) {
165                 if (epnt->linktype == NHLT_LINK_DMIC) {
166                         cfg = (struct nhlt_dmic_array_config  *)
167                                         (epnt->config.caps);
168                         switch (cfg->array_type) {
169                         case NHLT_MIC_ARRAY_2CH_SMALL:
170                         case NHLT_MIC_ARRAY_2CH_BIG:
171                                 dmic_geo |= MIC_ARRAY_2CH;
172                                 break;
173
174                         case NHLT_MIC_ARRAY_4CH_1ST_GEOM:
175                         case NHLT_MIC_ARRAY_4CH_L_SHAPED:
176                         case NHLT_MIC_ARRAY_4CH_2ND_GEOM:
177                                 dmic_geo |= MIC_ARRAY_4CH;
178                                 break;
179
180                         default:
181                                 dev_warn(dev, "undefined DMIC array_type 0x%0x\n",
182                                                 cfg->array_type);
183
184                         }
185                 }
186                 epnt = (struct nhlt_endpoint *)((u8 *)epnt + epnt->length);
187         }
188
189         return dmic_geo;
190 }
191
192 static void skl_nhlt_trim_space(struct skl *skl)
193 {
194         char *s = skl->tplg_name;
195         int cnt;
196         int i;
197
198         cnt = 0;
199         for (i = 0; s[i]; i++) {
200                 if (!isspace(s[i]))
201                         s[cnt++] = s[i];
202         }
203
204         s[cnt] = '\0';
205 }
206
207 int skl_nhlt_update_topology_bin(struct skl *skl)
208 {
209         struct nhlt_acpi_table *nhlt = (struct nhlt_acpi_table *)skl->nhlt;
210         struct hdac_bus *bus = ebus_to_hbus(&skl->ebus);
211         struct device *dev = bus->dev;
212
213         dev_dbg(dev, "oem_id %.6s, oem_table_id %8s oem_revision %d\n",
214                 nhlt->header.oem_id, nhlt->header.oem_table_id,
215                 nhlt->header.oem_revision);
216
217         snprintf(skl->tplg_name, sizeof(skl->tplg_name), "%x-%.6s-%.8s-%d%s",
218                 skl->pci_id, nhlt->header.oem_id, nhlt->header.oem_table_id,
219                 nhlt->header.oem_revision, "-tplg.bin");
220
221         skl_nhlt_trim_space(skl);
222
223         return 0;
224 }