df318f50dd0ba49bab7eb19ad5eb6ad9ad11b38b
[linux-2.6-microblaze.git] / sound / soc / sof / sof-acpi-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/acpi.h>
12 #include <linux/firmware.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <sound/soc-acpi.h>
16 #include <sound/soc-acpi-intel-match.h>
17 #include <sound/sof.h>
18 #include "../intel/common/soc-intel-quirks.h"
19 #include "ops.h"
20
21 /* platform specific devices */
22 #include "intel/shim.h"
23
24 static char *fw_path;
25 module_param(fw_path, charp, 0444);
26 MODULE_PARM_DESC(fw_path, "alternate path for SOF firmware.");
27
28 static char *tplg_path;
29 module_param(tplg_path, charp, 0444);
30 MODULE_PARM_DESC(tplg_path, "alternate path for SOF topology.");
31
32 static int sof_acpi_debug;
33 module_param_named(sof_acpi_debug, sof_acpi_debug, int, 0444);
34 MODULE_PARM_DESC(sof_acpi_debug, "SOF ACPI debug options (0x0 all off)");
35
36 #define SOF_ACPI_DISABLE_PM_RUNTIME BIT(0)
37
38 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HASWELL)
39 static const struct sof_dev_desc sof_acpi_haswell_desc = {
40         .machines = snd_soc_acpi_intel_haswell_machines,
41         .resindex_lpe_base = 0,
42         .resindex_pcicfg_base = 1,
43         .resindex_imr_base = -1,
44         .irqindex_host_ipc = 0,
45         .chip_info = &hsw_chip_info,
46         .default_fw_path = "intel/sof",
47         .default_tplg_path = "intel/sof-tplg",
48         .nocodec_fw_filename = "sof-hsw.ri",
49         .nocodec_tplg_filename = "sof-hsw-nocodec.tplg",
50         .ops = &sof_hsw_ops,
51         .arch_ops = &sof_xtensa_arch_ops
52 };
53 #endif
54
55 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
56 static const struct sof_dev_desc sof_acpi_broadwell_desc = {
57         .machines = snd_soc_acpi_intel_broadwell_machines,
58         .resindex_lpe_base = 0,
59         .resindex_pcicfg_base = 1,
60         .resindex_imr_base = -1,
61         .irqindex_host_ipc = 0,
62         .chip_info = &bdw_chip_info,
63         .default_fw_path = "intel/sof",
64         .default_tplg_path = "intel/sof-tplg",
65         .nocodec_fw_filename = "sof-bdw.ri",
66         .nocodec_tplg_filename = "sof-bdw-nocodec.tplg",
67         .ops = &sof_bdw_ops,
68         .arch_ops = &sof_xtensa_arch_ops
69 };
70 #endif
71
72 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BAYTRAIL)
73
74 /* BYTCR uses different IRQ index */
75 static const struct sof_dev_desc sof_acpi_baytrailcr_desc = {
76         .machines = snd_soc_acpi_intel_baytrail_machines,
77         .resindex_lpe_base = 0,
78         .resindex_pcicfg_base = 1,
79         .resindex_imr_base = 2,
80         .irqindex_host_ipc = 0,
81         .chip_info = &byt_chip_info,
82         .default_fw_path = "intel/sof",
83         .default_tplg_path = "intel/sof-tplg",
84         .nocodec_fw_filename = "sof-byt.ri",
85         .nocodec_tplg_filename = "sof-byt-nocodec.tplg",
86         .ops = &sof_byt_ops,
87         .arch_ops = &sof_xtensa_arch_ops
88 };
89
90 static const struct sof_dev_desc sof_acpi_baytrail_desc = {
91         .machines = snd_soc_acpi_intel_baytrail_machines,
92         .resindex_lpe_base = 0,
93         .resindex_pcicfg_base = 1,
94         .resindex_imr_base = 2,
95         .irqindex_host_ipc = 5,
96         .chip_info = &byt_chip_info,
97         .default_fw_path = "intel/sof",
98         .default_tplg_path = "intel/sof-tplg",
99         .nocodec_fw_filename = "sof-byt.ri",
100         .nocodec_tplg_filename = "sof-byt-nocodec.tplg",
101         .ops = &sof_byt_ops,
102         .arch_ops = &sof_xtensa_arch_ops
103 };
104
105 static const struct sof_dev_desc sof_acpi_cherrytrail_desc = {
106         .machines = snd_soc_acpi_intel_cherrytrail_machines,
107         .resindex_lpe_base = 0,
108         .resindex_pcicfg_base = 1,
109         .resindex_imr_base = 2,
110         .irqindex_host_ipc = 5,
111         .chip_info = &cht_chip_info,
112         .default_fw_path = "intel/sof",
113         .default_tplg_path = "intel/sof-tplg",
114         .nocodec_fw_filename = "sof-cht.ri",
115         .nocodec_tplg_filename = "sof-cht-nocodec.tplg",
116         .ops = &sof_cht_ops,
117         .arch_ops = &sof_xtensa_arch_ops
118 };
119
120 #endif
121
122 static const struct dev_pm_ops sof_acpi_pm = {
123         SET_SYSTEM_SLEEP_PM_OPS(snd_sof_suspend, snd_sof_resume)
124         SET_RUNTIME_PM_OPS(snd_sof_runtime_suspend, snd_sof_runtime_resume,
125                            snd_sof_runtime_idle)
126 };
127
128 static void sof_acpi_probe_complete(struct device *dev)
129 {
130         if (sof_acpi_debug & SOF_ACPI_DISABLE_PM_RUNTIME)
131                 return;
132
133         /* allow runtime_pm */
134         pm_runtime_set_autosuspend_delay(dev, SND_SOF_SUSPEND_DELAY_MS);
135         pm_runtime_use_autosuspend(dev);
136         pm_runtime_enable(dev);
137 }
138
139 static int sof_acpi_probe(struct platform_device *pdev)
140 {
141         struct device *dev = &pdev->dev;
142         const struct sof_dev_desc *desc;
143         struct snd_soc_acpi_mach *mach;
144         struct snd_sof_pdata *sof_pdata;
145         const struct snd_sof_dsp_ops *ops;
146         int ret;
147
148         dev_dbg(&pdev->dev, "ACPI DSP detected");
149
150         sof_pdata = devm_kzalloc(dev, sizeof(*sof_pdata), GFP_KERNEL);
151         if (!sof_pdata)
152                 return -ENOMEM;
153
154         desc = device_get_match_data(dev);
155         if (!desc)
156                 return -ENODEV;
157
158 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BAYTRAIL)
159         if (desc == &sof_acpi_baytrail_desc && soc_intel_is_byt_cr(pdev))
160                 desc = &sof_acpi_baytrailcr_desc;
161 #endif
162
163         /* get ops for platform */
164         ops = desc->ops;
165         if (!ops) {
166                 dev_err(dev, "error: no matching ACPI descriptor ops\n");
167                 return -ENODEV;
168         }
169
170 #if IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)
171         /* force nocodec mode */
172         dev_warn(dev, "Force to use nocodec mode\n");
173         mach = devm_kzalloc(dev, sizeof(*mach), GFP_KERNEL);
174         if (!mach)
175                 return -ENOMEM;
176         ret = sof_nocodec_setup(dev, sof_pdata, mach, desc, ops);
177         if (ret < 0)
178                 return ret;
179 #else
180         /* find machine */
181         mach = snd_soc_acpi_find_machine(desc->machines);
182         if (!mach) {
183                 dev_warn(dev, "warning: No matching ASoC machine driver found\n");
184         } else {
185                 sof_pdata->fw_filename = mach->sof_fw_filename;
186                 sof_pdata->tplg_filename = mach->sof_tplg_filename;
187         }
188 #endif
189
190         if (mach) {
191                 mach->mach_params.platform = dev_name(dev);
192                 mach->mach_params.acpi_ipc_irq_index = desc->irqindex_host_ipc;
193         }
194
195         sof_pdata->machine = mach;
196         sof_pdata->desc = desc;
197         sof_pdata->dev = &pdev->dev;
198         sof_pdata->platform = dev_name(dev);
199
200         /* alternate fw and tplg filenames ? */
201         if (fw_path)
202                 sof_pdata->fw_filename_prefix = fw_path;
203         else
204                 sof_pdata->fw_filename_prefix =
205                         sof_pdata->desc->default_fw_path;
206
207         if (tplg_path)
208                 sof_pdata->tplg_filename_prefix = tplg_path;
209         else
210                 sof_pdata->tplg_filename_prefix =
211                         sof_pdata->desc->default_tplg_path;
212
213 #if IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)
214         /* set callback to enable runtime_pm */
215         sof_pdata->sof_probe_complete = sof_acpi_probe_complete;
216 #endif
217         /* call sof helper for DSP hardware probe */
218         ret = snd_sof_device_probe(dev, sof_pdata);
219         if (ret) {
220                 dev_err(dev, "error: failed to probe DSP hardware!\n");
221                 return ret;
222         }
223
224 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)
225         sof_acpi_probe_complete(dev);
226 #endif
227
228         return ret;
229 }
230
231 static int sof_acpi_remove(struct platform_device *pdev)
232 {
233         if (!(sof_acpi_debug & SOF_ACPI_DISABLE_PM_RUNTIME))
234                 pm_runtime_disable(&pdev->dev);
235
236         /* call sof helper for DSP hardware remove */
237         snd_sof_device_remove(&pdev->dev);
238
239         return 0;
240 }
241
242 static const struct acpi_device_id sof_acpi_match[] = {
243 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HASWELL)
244         { "INT33C8", (unsigned long)&sof_acpi_haswell_desc },
245 #endif
246 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
247         { "INT3438", (unsigned long)&sof_acpi_broadwell_desc },
248 #endif
249 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BAYTRAIL)
250         { "80860F28", (unsigned long)&sof_acpi_baytrail_desc },
251         { "808622A8", (unsigned long)&sof_acpi_cherrytrail_desc },
252 #endif
253         { }
254 };
255 MODULE_DEVICE_TABLE(acpi, sof_acpi_match);
256
257 /* acpi_driver definition */
258 static struct platform_driver snd_sof_acpi_driver = {
259         .probe = sof_acpi_probe,
260         .remove = sof_acpi_remove,
261         .driver = {
262                 .name = "sof-audio-acpi",
263                 .pm = &sof_acpi_pm,
264                 .acpi_match_table = ACPI_PTR(sof_acpi_match),
265         },
266 };
267 module_platform_driver(snd_sof_acpi_driver);
268
269 MODULE_LICENSE("Dual BSD/GPL");