ALSA: hda: add Intel DSP configuration / probe code
[linux-2.6-block.git] / sound / hda / intel-dsp-config.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Jaroslav Kysela <perex@perex.cz>
3
4 #include <linux/bits.h>
5 #include <linux/dmi.h>
6 #include <linux/module.h>
7 #include <linux/pci.h>
8 #include <sound/core.h>
9 #include <sound/intel-dsp-config.h>
10 #include <sound/intel-nhlt.h>
11
12 static int dsp_driver;
13
14 module_param(dsp_driver, int, 0444);
15 MODULE_PARM_DESC(dsp_driver, "Force the DSP driver for Intel DSP (0=auto, 1=legacy, 2=SST, 3=SOF)");
16
17 #define FLAG_SST                BIT(0)
18 #define FLAG_SOF                BIT(1)
19 #define FLAG_SOF_ONLY_IF_DMIC   BIT(16)
20
21 struct config_entry {
22         u32 flags;
23         u16 device;
24         const struct dmi_system_id *dmi_table;
25 };
26
27 /*
28  * configuration table
29  * - the order of similar PCI ID entries is important!
30  * - the first successful match will win
31  */
32 static const struct config_entry config_table[] = {
33 /* Cometlake-LP */
34 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CML_LP)
35         {
36                 /* prefer SST */
37                 .flags = FLAG_SST,
38                 .device = 0x02c8,
39         },
40 #elif IS_ENABLED(CONFIG_SND_SOC_SOF_COMETLAKE_LP)
41         {
42                 .flags = FLAG_SOF,
43                 .device = 0x02c8,
44         },
45 #endif
46 /* Cometlake-H */
47 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_CML_H)
48         {
49                 .flags = FLAG_SST,
50                 .device = 0x06c8,
51         },
52 #elif IS_ENABLED(CONFIG_SND_SOC_SOF_COMETLAKE_H)
53         {
54                 .flags = FLAG_SOF,
55                 .device = 0x06c8,
56         },
57 #endif
58 /* Merrifield */
59 #if IS_ENABLED(CONFIG_SND_SOC_SOF_MERRIFIELD)
60         {
61                 .flags = FLAG_SOF,
62                 .device = 0x119a,
63         },
64 #endif
65 /* Broxton-T */
66 #if IS_ENABLED(CONFIG_SND_SOC_SOF_APOLLOLAKE)
67         {
68                 .flags = FLAG_SOF | FLAG_SOF_ONLY_IF_DMIC,
69                 .device = 0x1a98,
70         },
71 #endif
72 /* Geminilake */
73 #if IS_ENABLED(CONFIG_SND_SOC_SOF_GEMINILAKE)
74         {
75                 .flags = FLAG_SOF,
76                 .device = 0x3198,
77                 .dmi_table = (const struct dmi_system_id []) {
78                         {
79                                 .ident = "Google Chromebooks",
80                                 .matches = {
81                                         DMI_MATCH(DMI_SYS_VENDOR, "Google"),
82                                 }
83                         },
84                         {}
85                 }
86         },
87 #endif
88 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_GLK)
89         {
90                 .flags = FLAG_SST,
91                 .device = 0x3198,
92         },
93 #endif
94 /* Icelake */
95 #if IS_ENABLED(CONFIG_SND_SOC_SOF_ICELAKE)
96         {
97                 .flags = FLAG_SOF | FLAG_SOF_ONLY_IF_DMIC,
98                 .device = 0x34c8,
99         },
100 #endif
101 /* Elkhart Lake */
102 #if IS_ENABLED(CONFIG_SND_SOC_SOF_ELKHARTLAKE)
103         {
104                 .flags = FLAG_SOF | FLAG_SOF_ONLY_IF_DMIC,
105                 .device = 0x4b55,
106         },
107 #endif
108 /* Appololake (Broxton-P) */
109 #if IS_ENABLED(CONFIG_SND_SOC_SOF_APOLLOLAKE)
110         {
111                 .flags = FLAG_SOF,
112                 .device = 0x5a98,
113                 .dmi_table = (const struct dmi_system_id []) {
114                         {
115                                 .ident = "Up Squared",
116                                 .matches = {
117                                         DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
118                                         DMI_MATCH(DMI_BOARD_NAME, "UP-APL01"),
119                                 }
120                         },
121                         {}
122                 }
123         },
124 #endif
125 #if IS_ENABLED(CONFIG_SND_SOC_INTEL_APL)
126         {
127                 .flags = FLAG_SST,
128                 .device = 0x5a98,
129         },
130 #endif
131 /* Cannonlake */
132 #if IS_ENABLED(CONFIG_SND_SOC_SOF_CANNONLAKE)
133         {
134                 .flags = FLAG_SOF | FLAG_SOF_ONLY_IF_DMIC,
135                 .device = 0x9dc8,
136         },
137 #endif
138 /* Sunrise Point-LP */
139 #if IS_ENABLED(CONFIG_SND_SOC_SOF_SKYLAKE)
140         {
141                 .flags = FLAG_SOF | FLAG_SOF_ONLY_IF_DMIC,
142                 .device = 0x9d70,
143         },
144 #endif
145 /* Kabylake-LP */
146 #if IS_ENABLED(CONFIG_SND_SOC_SOF_KABYLAKE)
147         {
148                 .flags = FLAG_SOF | FLAG_SOF_ONLY_IF_DMIC,
149                 .device = 0x9d71,
150         },
151 #endif
152 /* Tigerlake */
153 #if IS_ENABLED(CONFIG_SND_SOC_SOF_TIGERLAKE)
154         {
155                 .flags = FLAG_SOF | FLAG_SOF_ONLY_IF_DMIC,
156                 .device = 0xa0c8,
157         },
158 #endif
159 /* Coffelake */
160 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COFFEELAKE)
161         {
162                 .flags = FLAG_SOF | FLAG_SOF_ONLY_IF_DMIC,
163                 .device = 0xa348,
164         },
165 #endif
166 };
167
168 static const struct config_entry *snd_intel_dsp_find_config
169                 (struct pci_dev *pci, const struct config_entry *table, u32 len)
170 {
171         u16 device;
172
173         device = pci->device;
174         for (; len > 0; len--, table++) {
175                 if (table->device != device)
176                         continue;
177                 if (table->dmi_table && !dmi_check_system(table->dmi_table))
178                         continue;
179                 return table;
180         }
181         return NULL;
182 }
183
184 static int snd_intel_dsp_check_dmic(struct pci_dev *pci)
185 {
186         struct nhlt_acpi_table *nhlt;
187         int ret = 0;
188
189         nhlt = intel_nhlt_init(&pci->dev);
190         if (nhlt) {
191                 if (intel_nhlt_get_dmic_geo(&pci->dev, nhlt))
192                         ret = 1;
193                 intel_nhlt_free(nhlt);
194         }
195         return ret;
196 }
197
198 int snd_intel_dsp_driver_probe(struct pci_dev *pci)
199 {
200         const struct config_entry *cfg;
201
202         if (dsp_driver > 0 && dsp_driver <= SND_INTEL_DSP_DRIVER_LAST)
203                 return dsp_driver;
204
205         /* Intel vendor only */
206         if (snd_BUG_ON(pci->vendor != 0x8086))
207                 return SND_INTEL_DSP_DRIVER_ANY;
208
209         /*
210          * detect DSP by checking class/subclass/prog-id information
211          * class=04 subclass 03 prog-if 00: no DSP, use legacy driver
212          * class=04 subclass 01 prog-if 00: DSP is present
213          *  (and may be required e.g. for DMIC or SSP support)
214          * class=04 subclass 03 prog-if 80: use DSP or legacy mode
215          */
216         if (pci->class == 0x040300)
217                 return SND_INTEL_DSP_DRIVER_LEGACY;
218         if (pci->class != 0x040100 && pci->class != 0x040380) {
219                 dev_err(&pci->dev, "Unknown PCI class/subclass/prog-if information (0x%06x) found, selecting HDA legacy driver\n", pci->class);
220                 return SND_INTEL_DSP_DRIVER_LEGACY;
221         }
222
223         dev_info(&pci->dev, "DSP detected with PCI class/subclass/prog-if info 0x%06x\n", pci->class);
224
225         /* find the configuration for the specific device */
226         cfg = snd_intel_dsp_find_config(pci, config_table, ARRAY_SIZE(config_table));
227         if (!cfg)
228                 return SND_INTEL_DSP_DRIVER_ANY;
229
230         if (cfg->flags & FLAG_SOF) {
231                 if (cfg->flags & FLAG_SOF_ONLY_IF_DMIC) {
232                         if (snd_intel_dsp_check_dmic(pci)) {
233                                 dev_info(&pci->dev, "Digital mics found on Skylake+ platform, using SOF driver\n");
234                                 return SND_INTEL_DSP_DRIVER_SOF;
235                         }
236                 } else {
237                         return SND_INTEL_DSP_DRIVER_SOF;
238                 }
239         }
240
241         if (cfg->flags & FLAG_SST)
242                 return SND_INTEL_DSP_DRIVER_SST;
243
244         return SND_INTEL_DSP_DRIVER_LEGACY;
245 }
246 EXPORT_SYMBOL_GPL(snd_intel_dsp_driver_probe);
247
248 MODULE_LICENSE("GPL v2");
249 MODULE_DESCRIPTION("Intel DSP config driver");