This source file includes following definitions.
- sof_restore_kcontrols
- sof_restore_pipelines
- sof_send_pm_ipc
- sof_set_hw_params_upon_resume
- sof_cache_debugfs
- sof_resume
- sof_suspend
- snd_sof_runtime_suspend
- snd_sof_runtime_idle
- snd_sof_runtime_resume
- snd_sof_resume
- snd_sof_suspend
1
2
3
4
5
6
7
8
9
10
11 #include "ops.h"
12 #include "sof-priv.h"
13
14 static int sof_restore_kcontrols(struct snd_sof_dev *sdev)
15 {
16 struct snd_sof_control *scontrol;
17 int ipc_cmd, ctrl_type;
18 int ret = 0;
19
20
21 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
22
23 scontrol->readback_offset = 0;
24
25
26 switch (scontrol->cmd) {
27 case SOF_CTRL_CMD_VOLUME:
28 case SOF_CTRL_CMD_ENUM:
29 case SOF_CTRL_CMD_SWITCH:
30 ipc_cmd = SOF_IPC_COMP_SET_VALUE;
31 ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
32 ret = snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
33 ipc_cmd, ctrl_type,
34 scontrol->cmd,
35 true);
36 break;
37 case SOF_CTRL_CMD_BINARY:
38 ipc_cmd = SOF_IPC_COMP_SET_DATA;
39 ctrl_type = SOF_CTRL_TYPE_DATA_SET;
40 ret = snd_sof_ipc_set_get_comp_data(sdev->ipc, scontrol,
41 ipc_cmd, ctrl_type,
42 scontrol->cmd,
43 true);
44 break;
45
46 default:
47 break;
48 }
49
50 if (ret < 0) {
51 dev_err(sdev->dev,
52 "error: failed kcontrol value set for widget: %d\n",
53 scontrol->comp_id);
54
55 return ret;
56 }
57 }
58
59 return 0;
60 }
61
62 static int sof_restore_pipelines(struct snd_sof_dev *sdev)
63 {
64 struct snd_sof_widget *swidget;
65 struct snd_sof_route *sroute;
66 struct sof_ipc_pipe_new *pipeline;
67 struct snd_sof_dai *dai;
68 struct sof_ipc_comp_dai *comp_dai;
69 struct sof_ipc_cmd_hdr *hdr;
70 int ret;
71
72
73 list_for_each_entry_reverse(swidget, &sdev->widget_list, list) {
74 struct sof_ipc_comp_reply r;
75
76
77 if (!swidget->private)
78 continue;
79
80 switch (swidget->id) {
81 case snd_soc_dapm_dai_in:
82 case snd_soc_dapm_dai_out:
83 dai = swidget->private;
84 comp_dai = &dai->comp_dai;
85 ret = sof_ipc_tx_message(sdev->ipc,
86 comp_dai->comp.hdr.cmd,
87 comp_dai, sizeof(*comp_dai),
88 &r, sizeof(r));
89 break;
90 case snd_soc_dapm_scheduler:
91
92
93
94
95
96
97
98 pipeline = swidget->private;
99 ret = sof_load_pipeline_ipc(sdev, pipeline, &r);
100 break;
101 default:
102 hdr = swidget->private;
103 ret = sof_ipc_tx_message(sdev->ipc, hdr->cmd,
104 swidget->private, hdr->size,
105 &r, sizeof(r));
106 break;
107 }
108 if (ret < 0) {
109 dev_err(sdev->dev,
110 "error: failed to load widget type %d with ID: %d\n",
111 swidget->widget->id, swidget->comp_id);
112
113 return ret;
114 }
115 }
116
117
118 list_for_each_entry_reverse(sroute, &sdev->route_list, list) {
119 struct sof_ipc_pipe_comp_connect *connect;
120 struct sof_ipc_reply reply;
121
122
123 if (!sroute->private)
124 continue;
125
126 connect = sroute->private;
127
128
129 ret = sof_ipc_tx_message(sdev->ipc,
130 connect->hdr.cmd,
131 connect, sizeof(*connect),
132 &reply, sizeof(reply));
133 if (ret < 0) {
134 dev_err(sdev->dev,
135 "error: failed to load route sink %s control %s source %s\n",
136 sroute->route->sink,
137 sroute->route->control ? sroute->route->control
138 : "none",
139 sroute->route->source);
140
141 return ret;
142 }
143 }
144
145
146 list_for_each_entry_reverse(dai, &sdev->dai_list, list) {
147 struct sof_ipc_reply reply;
148 struct sof_ipc_dai_config *config = dai->dai_config;
149
150 if (!config) {
151 dev_err(sdev->dev, "error: no config for DAI %s\n",
152 dai->name);
153 continue;
154 }
155
156
157
158
159
160
161
162 if (config->type == SOF_DAI_INTEL_HDA)
163 config->hda.link_dma_ch = DMA_CHAN_INVALID;
164
165 ret = sof_ipc_tx_message(sdev->ipc,
166 config->hdr.cmd, config,
167 config->hdr.size,
168 &reply, sizeof(reply));
169
170 if (ret < 0) {
171 dev_err(sdev->dev,
172 "error: failed to set dai config for %s\n",
173 dai->name);
174
175 return ret;
176 }
177 }
178
179
180 list_for_each_entry(swidget, &sdev->widget_list, list) {
181 switch (swidget->id) {
182 case snd_soc_dapm_scheduler:
183 swidget->complete =
184 snd_sof_complete_pipeline(sdev, swidget);
185 break;
186 default:
187 break;
188 }
189 }
190
191
192 ret = sof_restore_kcontrols(sdev);
193 if (ret < 0)
194 dev_err(sdev->dev,
195 "error: restoring kcontrols after resume\n");
196
197 return ret;
198 }
199
200 static int sof_send_pm_ipc(struct snd_sof_dev *sdev, int cmd)
201 {
202 struct sof_ipc_pm_ctx pm_ctx;
203 struct sof_ipc_reply reply;
204
205 memset(&pm_ctx, 0, sizeof(pm_ctx));
206
207
208 pm_ctx.hdr.size = sizeof(pm_ctx);
209 pm_ctx.hdr.cmd = SOF_IPC_GLB_PM_MSG | cmd;
210
211
212 return sof_ipc_tx_message(sdev->ipc, pm_ctx.hdr.cmd, &pm_ctx,
213 sizeof(pm_ctx), &reply, sizeof(reply));
214 }
215
216 static int sof_set_hw_params_upon_resume(struct snd_sof_dev *sdev)
217 {
218 struct snd_pcm_substream *substream;
219 struct snd_sof_pcm *spcm;
220 snd_pcm_state_t state;
221 int dir;
222
223
224
225
226
227
228 list_for_each_entry(spcm, &sdev->pcm_list, list) {
229 for (dir = 0; dir <= SNDRV_PCM_STREAM_CAPTURE; dir++) {
230 substream = spcm->stream[dir].substream;
231 if (!substream || !substream->runtime)
232 continue;
233
234 state = substream->runtime->status->state;
235 if (state == SNDRV_PCM_STATE_SUSPENDED)
236 spcm->prepared[dir] = false;
237 }
238 }
239
240
241 return snd_sof_dsp_hw_params_upon_resume(sdev);
242 }
243
244 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
245 static void sof_cache_debugfs(struct snd_sof_dev *sdev)
246 {
247 struct snd_sof_dfsentry *dfse;
248
249 list_for_each_entry(dfse, &sdev->dfsentry_list, list) {
250
251
252 if (dfse->type == SOF_DFSENTRY_TYPE_BUF)
253 continue;
254
255
256 if (dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY)
257 memcpy_fromio(dfse->cache_buf, dfse->io_mem,
258 dfse->size);
259 }
260 }
261 #endif
262
263 static int sof_resume(struct device *dev, bool runtime_resume)
264 {
265 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
266 int ret;
267
268
269 if (!sof_ops(sdev)->resume || !sof_ops(sdev)->runtime_resume)
270 return 0;
271
272
273 if (sdev->first_boot)
274 return 0;
275
276
277
278
279
280 if (runtime_resume)
281 ret = snd_sof_dsp_runtime_resume(sdev);
282 else
283 ret = snd_sof_dsp_resume(sdev);
284 if (ret < 0) {
285 dev_err(sdev->dev,
286 "error: failed to power up DSP after resume\n");
287 return ret;
288 }
289
290 sdev->fw_state = SOF_FW_BOOT_PREPARE;
291
292
293 ret = snd_sof_load_firmware(sdev);
294 if (ret < 0) {
295 dev_err(sdev->dev,
296 "error: failed to load DSP firmware after resume %d\n",
297 ret);
298 return ret;
299 }
300
301 sdev->fw_state = SOF_FW_BOOT_IN_PROGRESS;
302
303
304
305
306
307 ret = snd_sof_run_firmware(sdev);
308 if (ret < 0) {
309 dev_err(sdev->dev,
310 "error: failed to boot DSP firmware after resume %d\n",
311 ret);
312 return ret;
313 }
314
315
316 ret = snd_sof_init_trace_ipc(sdev);
317 if (ret < 0) {
318
319 dev_warn(sdev->dev,
320 "warning: failed to init trace after resume %d\n",
321 ret);
322 }
323
324
325 ret = sof_restore_pipelines(sdev);
326 if (ret < 0) {
327 dev_err(sdev->dev,
328 "error: failed to restore pipeline after resume %d\n",
329 ret);
330 return ret;
331 }
332
333
334 ret = sof_send_pm_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
335 if (ret < 0)
336 dev_err(sdev->dev,
337 "error: ctx_restore ipc error during resume %d\n",
338 ret);
339
340 return ret;
341 }
342
343 static int sof_suspend(struct device *dev, bool runtime_suspend)
344 {
345 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
346 int ret;
347
348
349 if (!sof_ops(sdev)->suspend)
350 return 0;
351
352 if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
353 goto power_down;
354
355
356 snd_sof_release_trace(sdev);
357
358
359 if (!runtime_suspend) {
360 ret = sof_set_hw_params_upon_resume(sdev);
361 if (ret < 0) {
362 dev_err(sdev->dev,
363 "error: setting hw_params flag during suspend %d\n",
364 ret);
365 return ret;
366 }
367 }
368
369 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
370
371 if (runtime_suspend)
372 sof_cache_debugfs(sdev);
373 #endif
374
375 ret = sof_send_pm_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
376 if (ret == -EBUSY || ret == -EAGAIN) {
377
378
379
380
381 dev_err(sdev->dev,
382 "error: ctx_save ipc error during suspend %d\n",
383 ret);
384 return ret;
385 } else if (ret < 0) {
386
387 dev_warn(sdev->dev,
388 "ctx_save ipc error %d, proceeding with suspend\n",
389 ret);
390 }
391
392 power_down:
393
394
395 if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
396 return 0;
397
398
399 if (runtime_suspend)
400 ret = snd_sof_dsp_runtime_suspend(sdev);
401 else
402 ret = snd_sof_dsp_suspend(sdev);
403 if (ret < 0)
404 dev_err(sdev->dev,
405 "error: failed to power down DSP during suspend %d\n",
406 ret);
407
408
409 sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
410
411 return ret;
412 }
413
414 int snd_sof_runtime_suspend(struct device *dev)
415 {
416 return sof_suspend(dev, true);
417 }
418 EXPORT_SYMBOL(snd_sof_runtime_suspend);
419
420 int snd_sof_runtime_idle(struct device *dev)
421 {
422 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
423
424 return snd_sof_dsp_runtime_idle(sdev);
425 }
426 EXPORT_SYMBOL(snd_sof_runtime_idle);
427
428 int snd_sof_runtime_resume(struct device *dev)
429 {
430 return sof_resume(dev, true);
431 }
432 EXPORT_SYMBOL(snd_sof_runtime_resume);
433
434 int snd_sof_resume(struct device *dev)
435 {
436 return sof_resume(dev, false);
437 }
438 EXPORT_SYMBOL(snd_sof_resume);
439
440 int snd_sof_suspend(struct device *dev)
441 {
442 return sof_suspend(dev, false);
443 }
444 EXPORT_SYMBOL(snd_sof_suspend);