ASoC: SOF: Make creation of machine device from SOF core optional
[linux-2.6-microblaze.git] / sound / soc / sof / sof-pci-dev.c
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license.  When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
7 //
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //
10
11 #include <linux/firmware.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/pm_runtime.h>
15 #include <sound/intel-dsp-config.h>
16 #include <sound/soc-acpi.h>
17 #include <sound/soc-acpi-intel-match.h>
18 #include <sound/sof.h>
19 #include "ops.h"
20
21 /* platform specific devices */
22 #include "intel/shim.h"
23 #include "intel/hda.h"
24
25 static char *fw_path;
26 module_param(fw_path, charp, 0444);
27 MODULE_PARM_DESC(fw_path, "alternate path for SOF firmware.");
28
29 static char *tplg_path;
30 module_param(tplg_path, charp, 0444);
31 MODULE_PARM_DESC(tplg_path, "alternate path for SOF topology.");
32
33 static int sof_pci_debug;
34 module_param_named(sof_pci_debug, sof_pci_debug, int, 0444);
35 MODULE_PARM_DESC(sof_pci_debug, "SOF PCI debug options (0x0 all off)");
36
37 #define SOF_PCI_DISABLE_PM_RUNTIME BIT(0)
38
39 #if IS_ENABLED(CONFIG_SND_SOC_SOF_APOLLOLAKE)
40 static const struct sof_dev_desc bxt_desc = {
41         .machines               = snd_soc_acpi_intel_bxt_machines,
42         .resindex_lpe_base      = 0,
43         .resindex_pcicfg_base   = -1,
44         .resindex_imr_base      = -1,
45         .irqindex_host_ipc      = -1,
46         .resindex_dma_base      = -1,
47         .chip_info = &apl_chip_info,
48         .default_fw_path = "intel/sof",
49         .default_tplg_path = "intel/sof-tplg",
50         .default_fw_filename = "sof-apl.ri",
51         .nocodec_fw_filename = "sof-apl.ri",
52         .nocodec_tplg_filename = "sof-apl-nocodec.tplg",
53         .ops = &sof_apl_ops,
54         .arch_ops = &sof_xtensa_arch_ops
55 };
56 #endif
57
58 #if IS_ENABLED(CONFIG_SND_SOC_SOF_GEMINILAKE)
59 static const struct sof_dev_desc glk_desc = {
60         .machines               = snd_soc_acpi_intel_glk_machines,
61         .resindex_lpe_base      = 0,
62         .resindex_pcicfg_base   = -1,
63         .resindex_imr_base      = -1,
64         .irqindex_host_ipc      = -1,
65         .resindex_dma_base      = -1,
66         .chip_info = &apl_chip_info,
67         .default_fw_path = "intel/sof",
68         .default_tplg_path = "intel/sof-tplg",
69         .default_fw_filename = "sof-glk.ri",
70         .nocodec_fw_filename = "sof-glk.ri",
71         .nocodec_tplg_filename = "sof-glk-nocodec.tplg",
72         .ops = &sof_apl_ops,
73         .arch_ops = &sof_xtensa_arch_ops
74 };
75 #endif
76
77 #if IS_ENABLED(CONFIG_SND_SOC_SOF_MERRIFIELD)
78 static struct snd_soc_acpi_mach sof_tng_machines[] = {
79         {
80                 .id = "INT343A",
81                 .drv_name = "edison",
82                 .sof_fw_filename = "sof-byt.ri",
83                 .sof_tplg_filename = "sof-byt.tplg",
84         },
85         {}
86 };
87
88 static const struct sof_dev_desc tng_desc = {
89         .machines               = sof_tng_machines,
90         .resindex_lpe_base      = 3,    /* IRAM, but subtract IRAM offset */
91         .resindex_pcicfg_base   = -1,
92         .resindex_imr_base      = 0,
93         .irqindex_host_ipc      = -1,
94         .resindex_dma_base      = -1,
95         .chip_info = &tng_chip_info,
96         .default_fw_path = "intel/sof",
97         .default_tplg_path = "intel/sof-tplg",
98         .default_fw_filename = "sof-byt.ri",
99         .nocodec_fw_filename = "sof-byt.ri",
100         .nocodec_tplg_filename = "sof-byt.tplg",
101         .ops = &sof_tng_ops,
102         .arch_ops = &sof_xtensa_arch_ops
103 };
104 #endif
105
106 #if IS_ENABLED(CONFIG_SND_SOC_SOF_CANNONLAKE)
107 static const struct sof_dev_desc cnl_desc = {
108         .machines               = snd_soc_acpi_intel_cnl_machines,
109         .resindex_lpe_base      = 0,
110         .resindex_pcicfg_base   = -1,
111         .resindex_imr_base      = -1,
112         .irqindex_host_ipc      = -1,
113         .resindex_dma_base      = -1,
114         .chip_info = &cnl_chip_info,
115         .default_fw_path = "intel/sof",
116         .default_tplg_path = "intel/sof-tplg",
117         .default_fw_filename = "sof-cnl.ri",
118         .nocodec_fw_filename = "sof-cnl.ri",
119         .nocodec_tplg_filename = "sof-cnl-nocodec.tplg",
120         .ops = &sof_cnl_ops,
121         .arch_ops = &sof_xtensa_arch_ops
122 };
123 #endif
124
125 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COFFEELAKE)
126 static const struct sof_dev_desc cfl_desc = {
127         .machines               = snd_soc_acpi_intel_cfl_machines,
128         .resindex_lpe_base      = 0,
129         .resindex_pcicfg_base   = -1,
130         .resindex_imr_base      = -1,
131         .irqindex_host_ipc      = -1,
132         .resindex_dma_base      = -1,
133         .chip_info = &cnl_chip_info,
134         .default_fw_path = "intel/sof",
135         .default_tplg_path = "intel/sof-tplg",
136         .default_fw_filename = "sof-cfl.ri",
137         .nocodec_fw_filename = "sof-cfl.ri",
138         .nocodec_tplg_filename = "sof-cnl-nocodec.tplg",
139         .ops = &sof_cnl_ops,
140         .arch_ops = &sof_xtensa_arch_ops
141 };
142 #endif
143
144 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMETLAKE_LP) || \
145         IS_ENABLED(CONFIG_SND_SOC_SOF_COMETLAKE_H)
146
147 static const struct sof_dev_desc cml_desc = {
148         .machines               = snd_soc_acpi_intel_cml_machines,
149         .resindex_lpe_base      = 0,
150         .resindex_pcicfg_base   = -1,
151         .resindex_imr_base      = -1,
152         .irqindex_host_ipc      = -1,
153         .resindex_dma_base      = -1,
154         .chip_info = &cnl_chip_info,
155         .default_fw_path = "intel/sof",
156         .default_tplg_path = "intel/sof-tplg",
157         .default_fw_filename = "sof-cml.ri",
158         .nocodec_fw_filename = "sof-cml.ri",
159         .nocodec_tplg_filename = "sof-cnl-nocodec.tplg",
160         .ops = &sof_cnl_ops,
161         .arch_ops = &sof_xtensa_arch_ops
162 };
163 #endif
164
165 #if IS_ENABLED(CONFIG_SND_SOC_SOF_ICELAKE)
166 static const struct sof_dev_desc icl_desc = {
167         .machines               = snd_soc_acpi_intel_icl_machines,
168         .resindex_lpe_base      = 0,
169         .resindex_pcicfg_base   = -1,
170         .resindex_imr_base      = -1,
171         .irqindex_host_ipc      = -1,
172         .resindex_dma_base      = -1,
173         .chip_info = &icl_chip_info,
174         .default_fw_path = "intel/sof",
175         .default_tplg_path = "intel/sof-tplg",
176         .default_fw_filename = "sof-icl.ri",
177         .nocodec_fw_filename = "sof-icl.ri",
178         .nocodec_tplg_filename = "sof-icl-nocodec.tplg",
179         .ops = &sof_cnl_ops,
180         .arch_ops = &sof_xtensa_arch_ops
181 };
182 #endif
183
184 #if IS_ENABLED(CONFIG_SND_SOC_SOF_TIGERLAKE)
185 static const struct sof_dev_desc tgl_desc = {
186         .machines               = snd_soc_acpi_intel_tgl_machines,
187         .resindex_lpe_base      = 0,
188         .resindex_pcicfg_base   = -1,
189         .resindex_imr_base      = -1,
190         .irqindex_host_ipc      = -1,
191         .resindex_dma_base      = -1,
192         .chip_info = &tgl_chip_info,
193         .default_fw_path = "intel/sof",
194         .default_tplg_path = "intel/sof-tplg",
195         .default_fw_filename = "sof-tgl.ri",
196         .nocodec_fw_filename = "sof-tgl.ri",
197         .nocodec_tplg_filename = "sof-tgl-nocodec.tplg",
198         .ops = &sof_cnl_ops,
199         .arch_ops = &sof_xtensa_arch_ops
200 };
201 #endif
202
203 #if IS_ENABLED(CONFIG_SND_SOC_SOF_ELKHARTLAKE)
204 static const struct sof_dev_desc ehl_desc = {
205         .machines               = snd_soc_acpi_intel_ehl_machines,
206         .resindex_lpe_base      = 0,
207         .resindex_pcicfg_base   = -1,
208         .resindex_imr_base      = -1,
209         .irqindex_host_ipc      = -1,
210         .resindex_dma_base      = -1,
211         .chip_info = &ehl_chip_info,
212         .default_fw_path = "intel/sof",
213         .default_tplg_path = "intel/sof-tplg",
214         .default_fw_filename = "sof-ehl.ri",
215         .nocodec_fw_filename = "sof-ehl.ri",
216         .nocodec_tplg_filename = "sof-ehl-nocodec.tplg",
217         .ops = &sof_cnl_ops,
218         .arch_ops = &sof_xtensa_arch_ops
219 };
220 #endif
221
222 #if IS_ENABLED(CONFIG_SND_SOC_SOF_JASPERLAKE)
223 static const struct sof_dev_desc jsl_desc = {
224         .machines               = snd_soc_acpi_intel_jsl_machines,
225         .resindex_lpe_base      = 0,
226         .resindex_pcicfg_base   = -1,
227         .resindex_imr_base      = -1,
228         .irqindex_host_ipc      = -1,
229         .resindex_dma_base      = -1,
230         .chip_info = &jsl_chip_info,
231         .default_fw_path = "intel/sof",
232         .default_tplg_path = "intel/sof-tplg",
233         .nocodec_fw_filename = "sof-jsl.ri",
234         .nocodec_tplg_filename = "sof-jsl-nocodec.tplg",
235         .ops = &sof_cnl_ops,
236         .arch_ops = &sof_xtensa_arch_ops
237 };
238 #endif
239
240 static const struct dev_pm_ops sof_pci_pm = {
241         .prepare = snd_sof_prepare,
242         .complete = snd_sof_complete,
243         SET_SYSTEM_SLEEP_PM_OPS(snd_sof_suspend, snd_sof_resume)
244         SET_RUNTIME_PM_OPS(snd_sof_runtime_suspend, snd_sof_runtime_resume,
245                            snd_sof_runtime_idle)
246 };
247
248 static void sof_pci_probe_complete(struct device *dev)
249 {
250         dev_dbg(dev, "Completing SOF PCI probe");
251
252         if (sof_pci_debug & SOF_PCI_DISABLE_PM_RUNTIME)
253                 return;
254
255         /* allow runtime_pm */
256         pm_runtime_set_autosuspend_delay(dev, SND_SOF_SUSPEND_DELAY_MS);
257         pm_runtime_use_autosuspend(dev);
258
259         /*
260          * runtime pm for pci device is "forbidden" by default.
261          * so call pm_runtime_allow() to enable it.
262          */
263         pm_runtime_allow(dev);
264
265         /* mark last_busy for pm_runtime to make sure not suspend immediately */
266         pm_runtime_mark_last_busy(dev);
267
268         /* follow recommendation in pci-driver.c to decrement usage counter */
269         pm_runtime_put_noidle(dev);
270 }
271
272 static int sof_pci_probe(struct pci_dev *pci,
273                          const struct pci_device_id *pci_id)
274 {
275         struct device *dev = &pci->dev;
276         const struct sof_dev_desc *desc =
277                 (const struct sof_dev_desc *)pci_id->driver_data;
278         struct snd_sof_pdata *sof_pdata;
279         const struct snd_sof_dsp_ops *ops;
280         int ret;
281
282         ret = snd_intel_dsp_driver_probe(pci);
283         if (ret != SND_INTEL_DSP_DRIVER_ANY &&
284             ret != SND_INTEL_DSP_DRIVER_SOF)
285                 return -ENODEV;
286
287         dev_dbg(&pci->dev, "PCI DSP detected");
288
289         /* get ops for platform */
290         ops = desc->ops;
291         if (!ops) {
292                 dev_err(dev, "error: no matching PCI descriptor ops\n");
293                 return -ENODEV;
294         }
295
296         sof_pdata = devm_kzalloc(dev, sizeof(*sof_pdata), GFP_KERNEL);
297         if (!sof_pdata)
298                 return -ENOMEM;
299
300         ret = pcim_enable_device(pci);
301         if (ret < 0)
302                 return ret;
303
304         ret = pci_request_regions(pci, "Audio DSP");
305         if (ret < 0)
306                 return ret;
307
308         sof_pdata->name = pci_name(pci);
309         sof_pdata->desc = (struct sof_dev_desc *)pci_id->driver_data;
310         sof_pdata->dev = dev;
311         sof_pdata->fw_filename = desc->default_fw_filename;
312
313         /* alternate fw and tplg filenames ? */
314         if (fw_path)
315                 sof_pdata->fw_filename_prefix = fw_path;
316         else
317                 sof_pdata->fw_filename_prefix =
318                         sof_pdata->desc->default_fw_path;
319
320         if (tplg_path)
321                 sof_pdata->tplg_filename_prefix = tplg_path;
322         else
323                 sof_pdata->tplg_filename_prefix =
324                         sof_pdata->desc->default_tplg_path;
325
326 #if IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)
327         /* set callback to enable runtime_pm */
328         sof_pdata->sof_probe_complete = sof_pci_probe_complete;
329 #endif
330         /* call sof helper for DSP hardware probe */
331         ret = snd_sof_device_probe(dev, sof_pdata);
332         if (ret) {
333                 dev_err(dev, "error: failed to probe DSP hardware!\n");
334                 goto release_regions;
335         }
336
337 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)
338         sof_pci_probe_complete(dev);
339 #endif
340
341         return ret;
342
343 release_regions:
344         pci_release_regions(pci);
345
346         return ret;
347 }
348
349 static void sof_pci_remove(struct pci_dev *pci)
350 {
351         /* call sof helper for DSP hardware remove */
352         snd_sof_device_remove(&pci->dev);
353
354         /* follow recommendation in pci-driver.c to increment usage counter */
355         if (!(sof_pci_debug & SOF_PCI_DISABLE_PM_RUNTIME))
356                 pm_runtime_get_noresume(&pci->dev);
357
358         /* release pci regions and disable device */
359         pci_release_regions(pci);
360 }
361
362 /* PCI IDs */
363 static const struct pci_device_id sof_pci_ids[] = {
364 #if IS_ENABLED(CONFIG_SND_SOC_SOF_MERRIFIELD)
365         { PCI_DEVICE(0x8086, 0x119a),
366                 .driver_data = (unsigned long)&tng_desc},
367 #endif
368 #if IS_ENABLED(CONFIG_SND_SOC_SOF_APOLLOLAKE)
369         /* BXT-P & Apollolake */
370         { PCI_DEVICE(0x8086, 0x5a98),
371                 .driver_data = (unsigned long)&bxt_desc},
372         { PCI_DEVICE(0x8086, 0x1a98),
373                 .driver_data = (unsigned long)&bxt_desc},
374 #endif
375 #if IS_ENABLED(CONFIG_SND_SOC_SOF_GEMINILAKE)
376         { PCI_DEVICE(0x8086, 0x3198),
377                 .driver_data = (unsigned long)&glk_desc},
378 #endif
379 #if IS_ENABLED(CONFIG_SND_SOC_SOF_CANNONLAKE)
380         { PCI_DEVICE(0x8086, 0x9dc8),
381                 .driver_data = (unsigned long)&cnl_desc},
382 #endif
383 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COFFEELAKE)
384         { PCI_DEVICE(0x8086, 0xa348),
385                 .driver_data = (unsigned long)&cfl_desc},
386 #endif
387 #if IS_ENABLED(CONFIG_SND_SOC_SOF_ICELAKE)
388         { PCI_DEVICE(0x8086, 0x34C8),
389                 .driver_data = (unsigned long)&icl_desc},
390 #endif
391 #if IS_ENABLED(CONFIG_SND_SOC_SOF_JASPERLAKE)
392         { PCI_DEVICE(0x8086, 0x38c8),
393                 .driver_data = (unsigned long)&jsl_desc},
394 #endif
395 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMETLAKE_LP)
396         { PCI_DEVICE(0x8086, 0x02c8),
397                 .driver_data = (unsigned long)&cml_desc},
398 #endif
399 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMETLAKE_H)
400         { PCI_DEVICE(0x8086, 0x06c8),
401                 .driver_data = (unsigned long)&cml_desc},
402 #endif
403 #if IS_ENABLED(CONFIG_SND_SOC_SOF_TIGERLAKE)
404         { PCI_DEVICE(0x8086, 0xa0c8),
405                 .driver_data = (unsigned long)&tgl_desc},
406 #endif
407 #if IS_ENABLED(CONFIG_SND_SOC_SOF_ELKHARTLAKE)
408         { PCI_DEVICE(0x8086, 0x4b55),
409                 .driver_data = (unsigned long)&ehl_desc},
410 #endif
411         { 0, }
412 };
413 MODULE_DEVICE_TABLE(pci, sof_pci_ids);
414
415 /* pci_driver definition */
416 static struct pci_driver snd_sof_pci_driver = {
417         .name = "sof-audio-pci",
418         .id_table = sof_pci_ids,
419         .probe = sof_pci_probe,
420         .remove = sof_pci_remove,
421         .driver = {
422                 .pm = &sof_pci_pm,
423         },
424 };
425 module_pci_driver(snd_sof_pci_driver);
426
427 MODULE_LICENSE("Dual BSD/GPL");