Merge tag 'selinux-pr-20220523' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / sound / soc / sof / sof-pci-dev.c
CommitLineData
e149ca29 1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
a226893b
LG
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>
46207ca2 12#include <linux/dmi.h>
a226893b
LG
13#include <linux/module.h>
14#include <linux/pci.h>
15#include <linux/pm_runtime.h>
16#include <sound/soc-acpi.h>
17#include <sound/soc-acpi-intel-match.h>
18#include <sound/sof.h>
19#include "ops.h"
8d4ba1be 20#include "sof-pci-dev.h"
a226893b
LG
21
22static char *fw_path;
23module_param(fw_path, charp, 0444);
24MODULE_PARM_DESC(fw_path, "alternate path for SOF firmware.");
25
26static char *tplg_path;
27module_param(tplg_path, charp, 0444);
28MODULE_PARM_DESC(tplg_path, "alternate path for SOF topology.");
29
8b160dc2
PLB
30static int sof_pci_debug;
31module_param_named(sof_pci_debug, sof_pci_debug, int, 0444);
32MODULE_PARM_DESC(sof_pci_debug, "SOF PCI debug options (0x0 all off)");
33
5253a73d
SN
34static const char *sof_override_tplg_name;
35
8b160dc2
PLB
36#define SOF_PCI_DISABLE_PM_RUNTIME BIT(0)
37
5253a73d
SN
38static int sof_tplg_cb(const struct dmi_system_id *id)
39{
40 sof_override_tplg_name = id->driver_data;
41 return 1;
42}
43
44static const struct dmi_system_id sof_tplg_table[] = {
45 {
46 .callback = sof_tplg_cb,
47 .matches = {
48 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Volteer"),
2a4b91a2 49 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98373_ALC5682I_I2S_UP4"),
5253a73d
SN
50 },
51 .driver_data = "sof-tgl-rt5682-ssp0-max98373-ssp2.tplg",
52 },
46fa9a15 53 {
54 .callback = sof_tplg_cb,
55 .matches = {
56 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
57 DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
58 DMI_MATCH(DMI_OEM_STRING, "AUDIO-ADL_MAX98373_ALC5682I_I2S"),
59 },
60 .driver_data = "sof-adl-rt5682-ssp0-max98373-ssp2.tplg",
61 },
f316c9d9
MC
62 {
63 .callback = sof_tplg_cb,
64 .matches = {
65 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
66 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S"),
67 },
68 .driver_data = "sof-adl-max98390-ssp2-rt5682-ssp0.tplg",
69 },
b9afe038
AH
70 {
71 .callback = sof_tplg_cb,
72 .matches = {
73 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
74 DMI_MATCH(DMI_OEM_STRING, "AUDIO_AMP-MAX98360_ALC5682VS_I2S_2WAY"),
75 },
76 .driver_data = "sof-adl-max98360a-rt5682-2way.tplg",
77 },
24320c55
BL
78 {
79 .callback = sof_tplg_cb,
80 .matches = {
81 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
82 DMI_MATCH(DMI_OEM_STRING, "AUDIO-AUDIO_MAX98357_ALC5682I_I2S_2WAY"),
83 },
84 .driver_data = "sof-adl-max98357a-rt5682-2way.tplg",
85 },
fcd1e39c
AH
86 {
87 .callback = sof_tplg_cb,
88 .matches = {
89 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
90 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98360_ALC5682I_I2S_AMP_SSP2"),
91 },
92 .driver_data = "sof-adl-max98357a-rt5682.tplg",
93 },
5253a73d
SN
94 {}
95};
96
46207ca2
PLB
97static const struct dmi_system_id community_key_platforms[] = {
98 {
405e52f4 99 .ident = "Up boards",
46207ca2
PLB
100 .matches = {
101 DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
bd8036eb
PLB
102 }
103 },
46207ca2
PLB
104 {
105 .ident = "Google Chromebooks",
106 .matches = {
107 DMI_MATCH(DMI_SYS_VENDOR, "Google"),
108 }
109 },
110 {},
111};
112
8d4ba1be 113const struct dev_pm_ops sof_pci_pm = {
6d407a39
KJ
114 .prepare = snd_sof_prepare,
115 .complete = snd_sof_complete,
a226893b
LG
116 SET_SYSTEM_SLEEP_PM_OPS(snd_sof_suspend, snd_sof_resume)
117 SET_RUNTIME_PM_OPS(snd_sof_runtime_suspend, snd_sof_runtime_resume,
62fde977 118 snd_sof_runtime_idle)
a226893b 119};
8d4ba1be 120EXPORT_SYMBOL_NS(sof_pci_pm, SND_SOC_SOF_PCI_DEV);
a226893b
LG
121
122static void sof_pci_probe_complete(struct device *dev)
123{
124 dev_dbg(dev, "Completing SOF PCI probe");
125
8b160dc2
PLB
126 if (sof_pci_debug & SOF_PCI_DISABLE_PM_RUNTIME)
127 return;
128
a226893b
LG
129 /* allow runtime_pm */
130 pm_runtime_set_autosuspend_delay(dev, SND_SOF_SUSPEND_DELAY_MS);
131 pm_runtime_use_autosuspend(dev);
132
133 /*
134 * runtime pm for pci device is "forbidden" by default.
135 * so call pm_runtime_allow() to enable it.
136 */
137 pm_runtime_allow(dev);
138
f1b1b9b1
PX
139 /* mark last_busy for pm_runtime to make sure not suspend immediately */
140 pm_runtime_mark_last_busy(dev);
141
a226893b
LG
142 /* follow recommendation in pci-driver.c to decrement usage counter */
143 pm_runtime_put_noidle(dev);
144}
145
8d4ba1be 146int sof_pci_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
a226893b
LG
147{
148 struct device *dev = &pci->dev;
149 const struct sof_dev_desc *desc =
150 (const struct sof_dev_desc *)pci_id->driver_data;
a226893b 151 struct snd_sof_pdata *sof_pdata;
a226893b
LG
152 int ret;
153
154 dev_dbg(&pci->dev, "PCI DSP detected");
155
c61711c1
AKP
156 if (!desc) {
157 dev_err(dev, "error: no matching PCI descriptor\n");
158 return -ENODEV;
159 }
160
fd979ec1 161 if (!desc->ops) {
a226893b
LG
162 dev_err(dev, "error: no matching PCI descriptor ops\n");
163 return -ENODEV;
164 }
165
166 sof_pdata = devm_kzalloc(dev, sizeof(*sof_pdata), GFP_KERNEL);
167 if (!sof_pdata)
168 return -ENOMEM;
169
170 ret = pcim_enable_device(pci);
171 if (ret < 0)
172 return ret;
173
174 ret = pci_request_regions(pci, "Audio DSP");
175 if (ret < 0)
176 return ret;
177
a226893b 178 sof_pdata->name = pci_name(pci);
e5eaa4e6 179 sof_pdata->desc = desc;
a226893b 180 sof_pdata->dev = dev;
285880a2 181 sof_pdata->fw_filename = desc->default_fw_filename;
a226893b 182
46207ca2
PLB
183 /*
184 * for platforms using the SOF community key, change the
185 * default path automatically to pick the right files from the
186 * linux-firmware tree. This can be overridden with the
187 * fw_path kernel parameter, e.g. for developers.
188 */
189
a226893b 190 /* alternate fw and tplg filenames ? */
46207ca2 191 if (fw_path) {
a226893b 192 sof_pdata->fw_filename_prefix = fw_path;
46207ca2
PLB
193
194 dev_dbg(dev,
195 "Module parameter used, changed fw path to %s\n",
196 sof_pdata->fw_filename_prefix);
197
198 } else if (dmi_check_system(community_key_platforms)) {
199 sof_pdata->fw_filename_prefix =
200 devm_kasprintf(dev, GFP_KERNEL, "%s/%s",
201 sof_pdata->desc->default_fw_path,
202 "community");
203
204 dev_dbg(dev,
205 "Platform uses community key, changed fw path to %s\n",
206 sof_pdata->fw_filename_prefix);
207 } else {
a226893b
LG
208 sof_pdata->fw_filename_prefix =
209 sof_pdata->desc->default_fw_path;
46207ca2 210 }
a226893b
LG
211
212 if (tplg_path)
213 sof_pdata->tplg_filename_prefix = tplg_path;
214 else
215 sof_pdata->tplg_filename_prefix =
216 sof_pdata->desc->default_tplg_path;
217
5253a73d
SN
218 dmi_check_system(sof_tplg_table);
219 if (sof_override_tplg_name)
220 sof_pdata->tplg_filename = sof_override_tplg_name;
221
4d1284cd 222 /* set callback to be called on successful device probe to enable runtime_pm */
a226893b 223 sof_pdata->sof_probe_complete = sof_pci_probe_complete;
4d1284cd 224
a226893b
LG
225 /* call sof helper for DSP hardware probe */
226 ret = snd_sof_device_probe(dev, sof_pdata);
4d1284cd
PU
227 if (ret)
228 pci_release_regions(pci);
a226893b
LG
229
230 return ret;
231}
8d4ba1be 232EXPORT_SYMBOL_NS(sof_pci_probe, SND_SOC_SOF_PCI_DEV);
a226893b 233
8d4ba1be 234void sof_pci_remove(struct pci_dev *pci)
a226893b
LG
235{
236 /* call sof helper for DSP hardware remove */
237 snd_sof_device_remove(&pci->dev);
238
239 /* follow recommendation in pci-driver.c to increment usage counter */
271d9373
KV
240 if (snd_sof_device_probe_completed(&pci->dev) &&
241 !(sof_pci_debug & SOF_PCI_DISABLE_PM_RUNTIME))
8b160dc2 242 pm_runtime_get_noresume(&pci->dev);
a226893b
LG
243
244 /* release pci regions and disable device */
245 pci_release_regions(pci);
246}
8d4ba1be 247EXPORT_SYMBOL_NS(sof_pci_remove, SND_SOC_SOF_PCI_DEV);
a226893b 248
8d4ba1be 249void sof_pci_shutdown(struct pci_dev *pci)
3475b44c
KJ
250{
251 snd_sof_device_shutdown(&pci->dev);
252}
8d4ba1be 253EXPORT_SYMBOL_NS(sof_pci_shutdown, SND_SOC_SOF_PCI_DEV);
a226893b
LG
254
255MODULE_LICENSE("Dual BSD/GPL");