xref: /linux/sound/soc/sof/sof-acpi-dev.c (revision 44f57d78)
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 #ifdef CONFIG_X86
19 #include <asm/iosf_mbi.h>
20 #endif
21 
22 #include "ops.h"
23 
24 /* platform specific devices */
25 #include "intel/shim.h"
26 
27 static char *fw_path;
28 module_param(fw_path, charp, 0444);
29 MODULE_PARM_DESC(fw_path, "alternate path for SOF firmware.");
30 
31 static char *tplg_path;
32 module_param(tplg_path, charp, 0444);
33 MODULE_PARM_DESC(tplg_path, "alternate path for SOF topology.");
34 
35 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HASWELL)
36 static const struct sof_dev_desc sof_acpi_haswell_desc = {
37 	.machines = snd_soc_acpi_intel_haswell_machines,
38 	.resindex_lpe_base = 0,
39 	.resindex_pcicfg_base = 1,
40 	.resindex_imr_base = -1,
41 	.irqindex_host_ipc = 0,
42 	.chip_info = &hsw_chip_info,
43 	.default_fw_path = "intel/sof",
44 	.default_tplg_path = "intel/sof-tplg",
45 	.nocodec_fw_filename = "sof-hsw.ri",
46 	.nocodec_tplg_filename = "sof-hsw-nocodec.tplg",
47 	.ops = &sof_hsw_ops,
48 	.arch_ops = &sof_xtensa_arch_ops
49 };
50 #endif
51 
52 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
53 static const struct sof_dev_desc sof_acpi_broadwell_desc = {
54 	.machines = snd_soc_acpi_intel_broadwell_machines,
55 	.resindex_lpe_base = 0,
56 	.resindex_pcicfg_base = 1,
57 	.resindex_imr_base = -1,
58 	.irqindex_host_ipc = 0,
59 	.chip_info = &bdw_chip_info,
60 	.default_fw_path = "intel/sof",
61 	.default_tplg_path = "intel/sof-tplg",
62 	.nocodec_fw_filename = "sof-bdw.ri",
63 	.nocodec_tplg_filename = "sof-bdw-nocodec.tplg",
64 	.ops = &sof_bdw_ops,
65 	.arch_ops = &sof_xtensa_arch_ops
66 };
67 #endif
68 
69 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BAYTRAIL)
70 
71 /* BYTCR uses different IRQ index */
72 static const struct sof_dev_desc sof_acpi_baytrailcr_desc = {
73 	.machines = snd_soc_acpi_intel_baytrail_machines,
74 	.resindex_lpe_base = 0,
75 	.resindex_pcicfg_base = 1,
76 	.resindex_imr_base = 2,
77 	.irqindex_host_ipc = 0,
78 	.chip_info = &byt_chip_info,
79 	.default_fw_path = "intel/sof",
80 	.default_tplg_path = "intel/sof-tplg",
81 	.nocodec_fw_filename = "sof-byt.ri",
82 	.nocodec_tplg_filename = "sof-byt-nocodec.tplg",
83 	.ops = &sof_byt_ops,
84 	.arch_ops = &sof_xtensa_arch_ops
85 };
86 
87 static const struct sof_dev_desc sof_acpi_baytrail_desc = {
88 	.machines = snd_soc_acpi_intel_baytrail_machines,
89 	.resindex_lpe_base = 0,
90 	.resindex_pcicfg_base = 1,
91 	.resindex_imr_base = 2,
92 	.irqindex_host_ipc = 5,
93 	.chip_info = &byt_chip_info,
94 	.default_fw_path = "intel/sof",
95 	.default_tplg_path = "intel/sof-tplg",
96 	.nocodec_fw_filename = "sof-byt.ri",
97 	.nocodec_tplg_filename = "sof-byt-nocodec.tplg",
98 	.ops = &sof_byt_ops,
99 	.arch_ops = &sof_xtensa_arch_ops
100 };
101 
102 #ifdef CONFIG_X86 /* TODO: move this to common helper */
103 
104 static bool is_byt_cr(struct platform_device *pdev)
105 {
106 	struct device *dev = &pdev->dev;
107 	int status;
108 
109 	if (iosf_mbi_available()) {
110 		u32 bios_status;
111 		status = iosf_mbi_read(BT_MBI_UNIT_PMC, /* 0x04 PUNIT */
112 				       MBI_REG_READ, /* 0x10 */
113 				       0x006, /* BIOS_CONFIG */
114 				       &bios_status);
115 
116 		if (status) {
117 			dev_err(dev, "could not read PUNIT BIOS_CONFIG\n");
118 		} else {
119 			/* bits 26:27 mirror PMIC options */
120 			bios_status = (bios_status >> 26) & 3;
121 
122 			if (bios_status == 1 || bios_status == 3) {
123 				dev_info(dev, "Detected Baytrail-CR platform\n");
124 				return true;
125 			}
126 
127 			dev_info(dev, "BYT-CR not detected\n");
128 		}
129 	} else {
130 		dev_info(dev, "IOSF_MBI not available, no BYT-CR detection\n");
131 	}
132 
133 	if (platform_get_resource(pdev, IORESOURCE_IRQ, 5) == NULL) {
134 		/*
135 		 * Some devices detected as BYT-T have only a single IRQ listed,
136 		 * causing platform_get_irq with index 5 to return -ENXIO.
137 		 * The correct IRQ in this case is at index 0, as on BYT-CR.
138 		 */
139 		dev_info(dev, "Falling back to Baytrail-CR platform\n");
140 		return true;
141 	}
142 
143 	return false;
144 }
145 #else
146 static int is_byt_cr(struct platform_device *pdev)
147 {
148 	return 0;
149 }
150 #endif
151 
152 static const struct sof_dev_desc sof_acpi_cherrytrail_desc = {
153 	.machines = snd_soc_acpi_intel_cherrytrail_machines,
154 	.resindex_lpe_base = 0,
155 	.resindex_pcicfg_base = 1,
156 	.resindex_imr_base = 2,
157 	.irqindex_host_ipc = 5,
158 	.chip_info = &cht_chip_info,
159 	.default_fw_path = "intel/sof",
160 	.default_tplg_path = "intel/sof-tplg",
161 	.nocodec_fw_filename = "sof-cht.ri",
162 	.nocodec_tplg_filename = "sof-cht-nocodec.tplg",
163 	.ops = &sof_cht_ops,
164 	.arch_ops = &sof_xtensa_arch_ops
165 };
166 
167 #endif
168 
169 static const struct dev_pm_ops sof_acpi_pm = {
170 	SET_SYSTEM_SLEEP_PM_OPS(snd_sof_suspend, snd_sof_resume)
171 	SET_RUNTIME_PM_OPS(snd_sof_runtime_suspend, snd_sof_runtime_resume,
172 			   NULL)
173 };
174 
175 static void sof_acpi_probe_complete(struct device *dev)
176 {
177 	/* allow runtime_pm */
178 	pm_runtime_set_autosuspend_delay(dev, SND_SOF_SUSPEND_DELAY_MS);
179 	pm_runtime_use_autosuspend(dev);
180 	pm_runtime_enable(dev);
181 }
182 
183 static int sof_acpi_probe(struct platform_device *pdev)
184 {
185 	struct device *dev = &pdev->dev;
186 	const struct sof_dev_desc *desc;
187 	struct snd_soc_acpi_mach *mach;
188 	struct snd_sof_pdata *sof_pdata;
189 	const struct snd_sof_dsp_ops *ops;
190 	int ret;
191 
192 	dev_dbg(&pdev->dev, "ACPI DSP detected");
193 
194 	sof_pdata = devm_kzalloc(dev, sizeof(*sof_pdata), GFP_KERNEL);
195 	if (!sof_pdata)
196 		return -ENOMEM;
197 
198 	desc = device_get_match_data(dev);
199 	if (!desc)
200 		return -ENODEV;
201 
202 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BAYTRAIL)
203 	if (desc == &sof_acpi_baytrail_desc && is_byt_cr(pdev))
204 		desc = &sof_acpi_baytrailcr_desc;
205 #endif
206 
207 	/* get ops for platform */
208 	ops = desc->ops;
209 	if (!ops) {
210 		dev_err(dev, "error: no matching ACPI descriptor ops\n");
211 		return -ENODEV;
212 	}
213 
214 #if IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)
215 	/* force nocodec mode */
216 	dev_warn(dev, "Force to use nocodec mode\n");
217 	mach = devm_kzalloc(dev, sizeof(*mach), GFP_KERNEL);
218 	if (!mach)
219 		return -ENOMEM;
220 	ret = sof_nocodec_setup(dev, sof_pdata, mach, desc, ops);
221 	if (ret < 0)
222 		return ret;
223 #else
224 	/* find machine */
225 	mach = snd_soc_acpi_find_machine(desc->machines);
226 	if (!mach) {
227 		dev_warn(dev, "warning: No matching ASoC machine driver found\n");
228 	} else {
229 		sof_pdata->fw_filename = mach->sof_fw_filename;
230 		sof_pdata->tplg_filename = mach->sof_tplg_filename;
231 	}
232 #endif
233 
234 	if (mach) {
235 		mach->mach_params.platform = dev_name(dev);
236 		mach->mach_params.acpi_ipc_irq_index = desc->irqindex_host_ipc;
237 	}
238 
239 	sof_pdata->machine = mach;
240 	sof_pdata->desc = desc;
241 	sof_pdata->dev = &pdev->dev;
242 	sof_pdata->platform = dev_name(dev);
243 
244 	/* alternate fw and tplg filenames ? */
245 	if (fw_path)
246 		sof_pdata->fw_filename_prefix = fw_path;
247 	else
248 		sof_pdata->fw_filename_prefix =
249 			sof_pdata->desc->default_fw_path;
250 
251 	if (tplg_path)
252 		sof_pdata->tplg_filename_prefix = tplg_path;
253 	else
254 		sof_pdata->tplg_filename_prefix =
255 			sof_pdata->desc->default_tplg_path;
256 
257 #if IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)
258 	/* set callback to enable runtime_pm */
259 	sof_pdata->sof_probe_complete = sof_acpi_probe_complete;
260 #endif
261 	/* call sof helper for DSP hardware probe */
262 	ret = snd_sof_device_probe(dev, sof_pdata);
263 	if (ret) {
264 		dev_err(dev, "error: failed to probe DSP hardware!\n");
265 		return ret;
266 	}
267 
268 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)
269 	sof_acpi_probe_complete(dev);
270 #endif
271 
272 	return ret;
273 }
274 
275 static int sof_acpi_remove(struct platform_device *pdev)
276 {
277 	pm_runtime_disable(&pdev->dev);
278 
279 	/* call sof helper for DSP hardware remove */
280 	snd_sof_device_remove(&pdev->dev);
281 
282 	return 0;
283 }
284 
285 static const struct acpi_device_id sof_acpi_match[] = {
286 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HASWELL)
287 	{ "INT33C8", (unsigned long)&sof_acpi_haswell_desc },
288 #endif
289 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL)
290 	{ "INT3438", (unsigned long)&sof_acpi_broadwell_desc },
291 #endif
292 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BAYTRAIL)
293 	{ "80860F28", (unsigned long)&sof_acpi_baytrail_desc },
294 	{ "808622A8", (unsigned long)&sof_acpi_cherrytrail_desc },
295 #endif
296 	{ }
297 };
298 MODULE_DEVICE_TABLE(acpi, sof_acpi_match);
299 
300 /* acpi_driver definition */
301 static struct platform_driver snd_sof_acpi_driver = {
302 	.probe = sof_acpi_probe,
303 	.remove = sof_acpi_remove,
304 	.driver = {
305 		.name = "sof-audio-acpi",
306 		.pm = &sof_acpi_pm,
307 		.acpi_match_table = ACPI_PTR(sof_acpi_match),
308 	},
309 };
310 module_platform_driver(snd_sof_acpi_driver);
311 
312 MODULE_LICENSE("Dual BSD/GPL");
313