hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/sound/soc/soc-generic-dmaengine-pcm.c
....@@ -25,18 +25,6 @@
2525 */
2626 #define SND_DMAENGINE_PCM_FLAG_NO_RESIDUE BIT(31)
2727
28
-struct dmaengine_pcm {
29
- struct dma_chan *chan[SNDRV_PCM_STREAM_LAST + 1];
30
- const struct snd_dmaengine_pcm_config *config;
31
- struct snd_soc_component component;
32
- unsigned int flags;
33
-};
34
-
35
-static struct dmaengine_pcm *soc_component_to_pcm(struct snd_soc_component *p)
36
-{
37
- return container_of(p, struct dmaengine_pcm, component);
38
-}
39
-
4028 static struct device *dmaengine_dma_dev(struct dmaengine_pcm *pcm,
4129 struct snd_pcm_substream *substream)
4230 {
....@@ -62,11 +50,17 @@
6250 int snd_dmaengine_pcm_prepare_slave_config(struct snd_pcm_substream *substream,
6351 struct snd_pcm_hw_params *params, struct dma_slave_config *slave_config)
6452 {
65
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
53
+ struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
6654 struct snd_dmaengine_dai_dma_data *dma_data;
6755 int ret;
6856
69
- dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
57
+ if (rtd->num_cpus > 1) {
58
+ dev_err(rtd->dev,
59
+ "%s doesn't support Multi CPU yet\n", __func__);
60
+ return -EINVAL;
61
+ }
62
+
63
+ dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
7064
7165 ret = snd_hwparams_to_dma_slave_config(substream, params, slave_config);
7266 if (ret)
....@@ -79,12 +73,10 @@
7973 }
8074 EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_prepare_slave_config);
8175
82
-static int dmaengine_pcm_hw_params(struct snd_pcm_substream *substream,
83
- struct snd_pcm_hw_params *params)
76
+static int dmaengine_pcm_hw_params(struct snd_soc_component *component,
77
+ struct snd_pcm_substream *substream,
78
+ struct snd_pcm_hw_params *params)
8479 {
85
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
86
- struct snd_soc_component *component =
87
- snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
8880 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
8981 struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
9082 int (*prepare_slave_config)(struct snd_pcm_substream *substream,
....@@ -110,31 +102,31 @@
110102 return ret;
111103 }
112104
113
- return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
105
+ return 0;
114106 }
115107
116
-static int dmaengine_pcm_set_runtime_hwparams(struct snd_pcm_substream *substream)
108
+static int
109
+dmaengine_pcm_set_runtime_hwparams(struct snd_soc_component *component,
110
+ struct snd_pcm_substream *substream)
117111 {
118
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
119
- struct snd_soc_component *component =
120
- snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
112
+ struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
121113 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
122114 struct device *dma_dev = dmaengine_dma_dev(pcm, substream);
123115 struct dma_chan *chan = pcm->chan[substream->stream];
124116 struct snd_dmaengine_dai_dma_data *dma_data;
125
- struct dma_slave_caps dma_caps;
126117 struct snd_pcm_hardware hw;
127
- u32 addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
128
- BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
129
- BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
130
- snd_pcm_format_t i;
131
- int ret;
118
+
119
+ if (rtd->num_cpus > 1) {
120
+ dev_err(rtd->dev,
121
+ "%s doesn't support Multi CPU yet\n", __func__);
122
+ return -EINVAL;
123
+ }
132124
133125 if (pcm->config && pcm->config->pcm_hardware)
134126 return snd_soc_set_runtime_hwparams(substream,
135127 pcm->config->pcm_hardware);
136128
137
- dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
129
+ dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
138130
139131 memset(&hw, 0, sizeof(hw));
140132 hw.info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
....@@ -149,87 +141,61 @@
149141 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
150142 hw.info |= SNDRV_PCM_INFO_BATCH;
151143
152
- ret = dma_get_slave_caps(chan, &dma_caps);
153
- if (ret == 0) {
154
- if (dma_caps.cmd_pause && dma_caps.cmd_resume)
155
- hw.info |= SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME;
156
- if (dma_caps.residue_granularity <= DMA_RESIDUE_GRANULARITY_SEGMENT)
157
- hw.info |= SNDRV_PCM_INFO_BATCH;
158
-
159
- if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
160
- addr_widths = dma_caps.dst_addr_widths;
161
- else
162
- addr_widths = dma_caps.src_addr_widths;
163
- }
164
-
165
- /*
166
- * If SND_DMAENGINE_PCM_DAI_FLAG_PACK is set keep
167
- * hw.formats set to 0, meaning no restrictions are in place.
168
- * In this case it's the responsibility of the DAI driver to
169
- * provide the supported format information.
144
+ /**
145
+ * FIXME: Remove the return value check to align with the code
146
+ * before adding snd_dmaengine_pcm_refine_runtime_hwparams
147
+ * function.
170148 */
171
- if (!(dma_data->flags & SND_DMAENGINE_PCM_DAI_FLAG_PACK))
172
- /*
173
- * Prepare formats mask for valid/allowed sample types. If the
174
- * dma does not have support for the given physical word size,
175
- * it needs to be masked out so user space can not use the
176
- * format which produces corrupted audio.
177
- * In case the dma driver does not implement the slave_caps the
178
- * default assumption is that it supports 1, 2 and 4 bytes
179
- * widths.
180
- */
181
- for (i = SNDRV_PCM_FORMAT_FIRST; i <= SNDRV_PCM_FORMAT_LAST; i++) {
182
- int bits = snd_pcm_format_physical_width(i);
183
-
184
- /*
185
- * Enable only samples with DMA supported physical
186
- * widths
187
- */
188
- switch (bits) {
189
- case 8:
190
- case 16:
191
- case 24:
192
- case 32:
193
- case 64:
194
- if (addr_widths & (1 << (bits / 8)))
195
- hw.formats |= pcm_format_to_bits(i);
196
- break;
197
- default:
198
- /* Unsupported types */
199
- break;
200
- }
201
- }
149
+ snd_dmaengine_pcm_refine_runtime_hwparams(substream,
150
+ dma_data,
151
+ &hw,
152
+ chan);
202153
203154 return snd_soc_set_runtime_hwparams(substream, &hw);
204155 }
205156
206
-static int dmaengine_pcm_open(struct snd_pcm_substream *substream)
157
+static int dmaengine_pcm_open(struct snd_soc_component *component,
158
+ struct snd_pcm_substream *substream)
207159 {
208
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
209
- struct snd_soc_component *component =
210
- snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
211160 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
212161 struct dma_chan *chan = pcm->chan[substream->stream];
213162 int ret;
214163
215
- ret = dmaengine_pcm_set_runtime_hwparams(substream);
164
+ ret = dmaengine_pcm_set_runtime_hwparams(component, substream);
216165 if (ret)
217166 return ret;
218167
219168 return snd_dmaengine_pcm_open(substream, chan);
220169 }
221170
171
+static int dmaengine_pcm_close(struct snd_soc_component *component,
172
+ struct snd_pcm_substream *substream)
173
+{
174
+ return snd_dmaengine_pcm_close(substream);
175
+}
176
+
177
+static int dmaengine_pcm_trigger(struct snd_soc_component *component,
178
+ struct snd_pcm_substream *substream, int cmd)
179
+{
180
+ return snd_dmaengine_pcm_trigger(substream, cmd);
181
+}
182
+
222183 static struct dma_chan *dmaengine_pcm_compat_request_channel(
184
+ struct snd_soc_component *component,
223185 struct snd_soc_pcm_runtime *rtd,
224186 struct snd_pcm_substream *substream)
225187 {
226
- struct snd_soc_component *component =
227
- snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
228188 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
229189 struct snd_dmaengine_dai_dma_data *dma_data;
230190 dma_filter_fn fn = NULL;
231191
232
- dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
192
+ if (rtd->num_cpus > 1) {
193
+ dev_err(rtd->dev,
194
+ "%s doesn't support Multi CPU yet\n", __func__);
195
+ return NULL;
196
+ }
197
+
198
+ dma_data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
233199
234200 if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX) && pcm->chan[0])
235201 return pcm->chan[0];
....@@ -262,19 +228,16 @@
262228 return true;
263229 }
264230
265
-static int dmaengine_pcm_new(struct snd_soc_pcm_runtime *rtd)
231
+static int dmaengine_pcm_new(struct snd_soc_component *component,
232
+ struct snd_soc_pcm_runtime *rtd)
266233 {
267
- struct snd_soc_component *component =
268
- snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
269234 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
270235 const struct snd_dmaengine_pcm_config *config = pcm->config;
271236 struct device *dev = component->dev;
272
- struct snd_dmaengine_dai_dma_data *dma_data;
273237 struct snd_pcm_substream *substream;
274238 size_t prealloc_buffer_size;
275239 size_t max_buffer_size;
276240 unsigned int i;
277
- int ret;
278241
279242 if (config && config->prealloc_buffer_size) {
280243 prealloc_buffer_size = config->prealloc_buffer_size;
....@@ -284,21 +247,18 @@
284247 max_buffer_size = SIZE_MAX;
285248 }
286249
287
- for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE; i++) {
250
+ for_each_pcm_streams(i) {
288251 substream = rtd->pcm->streams[i].substream;
289252 if (!substream)
290253 continue;
291254
292
- dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
293
-
294
- if (!pcm->chan[i] &&
295
- (pcm->flags & SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME))
255
+ if (!pcm->chan[i] && config && config->chan_names[i])
296256 pcm->chan[i] = dma_request_slave_channel(dev,
297
- dma_data->chan_name);
257
+ config->chan_names[i]);
298258
299259 if (!pcm->chan[i] && (pcm->flags & SND_DMAENGINE_PCM_FLAG_COMPAT)) {
300
- pcm->chan[i] = dmaengine_pcm_compat_request_channel(rtd,
301
- substream);
260
+ pcm->chan[i] = dmaengine_pcm_compat_request_channel(
261
+ component, rtd, substream);
302262 }
303263
304264 if (!pcm->chan[i]) {
....@@ -307,21 +267,19 @@
307267 return -EINVAL;
308268 }
309269
310
- ret = snd_pcm_lib_preallocate_pages(substream,
270
+ snd_pcm_set_managed_buffer(substream,
311271 SNDRV_DMA_TYPE_DEV_IRAM,
312272 dmaengine_dma_dev(pcm, substream),
313273 prealloc_buffer_size,
314274 max_buffer_size);
315
- if (ret)
316
- return ret;
317275
318276 if (!dmaengine_pcm_can_report_residue(dev, pcm->chan[i]))
319277 pcm->flags |= SND_DMAENGINE_PCM_FLAG_NO_RESIDUE;
320278
321279 if (rtd->pcm->streams[i].pcm->name[0] == '\0') {
322
- strncpy(rtd->pcm->streams[i].pcm->name,
323
- rtd->pcm->streams[i].pcm->id,
324
- sizeof(rtd->pcm->streams[i].pcm->name));
280
+ strscpy_pad(rtd->pcm->streams[i].pcm->name,
281
+ rtd->pcm->streams[i].pcm->id,
282
+ sizeof(rtd->pcm->streams[i].pcm->name));
325283 }
326284 }
327285
....@@ -329,11 +287,9 @@
329287 }
330288
331289 static snd_pcm_uframes_t dmaengine_pcm_pointer(
290
+ struct snd_soc_component *component,
332291 struct snd_pcm_substream *substream)
333292 {
334
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
335
- struct snd_soc_component *component =
336
- snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
337293 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
338294
339295 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_RESIDUE)
....@@ -342,13 +298,11 @@
342298 return snd_dmaengine_pcm_pointer(substream);
343299 }
344300
345
-static int dmaengine_copy_user(struct snd_pcm_substream *substream,
301
+static int dmaengine_copy_user(struct snd_soc_component *component,
302
+ struct snd_pcm_substream *substream,
346303 int channel, unsigned long hwoff,
347304 void __user *buf, unsigned long bytes)
348305 {
349
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
350
- struct snd_soc_component *component =
351
- snd_soc_rtdcom_lookup(rtd, SND_DMAENGINE_PCM_DRV_NAME);
352306 struct snd_pcm_runtime *runtime = substream->runtime;
353307 struct dmaengine_pcm *pcm = soc_component_to_pcm(component);
354308 int (*process)(struct snd_pcm_substream *substream,
....@@ -376,39 +330,27 @@
376330 return 0;
377331 }
378332
379
-static const struct snd_pcm_ops dmaengine_pcm_ops = {
380
- .open = dmaengine_pcm_open,
381
- .close = snd_dmaengine_pcm_close,
382
- .ioctl = snd_pcm_lib_ioctl,
383
- .hw_params = dmaengine_pcm_hw_params,
384
- .hw_free = snd_pcm_lib_free_pages,
385
- .trigger = snd_dmaengine_pcm_trigger,
386
- .pointer = dmaengine_pcm_pointer,
387
-};
388
-
389
-static const struct snd_pcm_ops dmaengine_pcm_process_ops = {
390
- .open = dmaengine_pcm_open,
391
- .close = snd_dmaengine_pcm_close,
392
- .ioctl = snd_pcm_lib_ioctl,
393
- .hw_params = dmaengine_pcm_hw_params,
394
- .hw_free = snd_pcm_lib_free_pages,
395
- .trigger = snd_dmaengine_pcm_trigger,
396
- .pointer = dmaengine_pcm_pointer,
397
- .copy_user = dmaengine_copy_user,
398
-};
399
-
400333 static const struct snd_soc_component_driver dmaengine_pcm_component = {
401334 .name = SND_DMAENGINE_PCM_DRV_NAME,
402335 .probe_order = SND_SOC_COMP_ORDER_LATE,
403
- .ops = &dmaengine_pcm_ops,
404
- .pcm_new = dmaengine_pcm_new,
336
+ .open = dmaengine_pcm_open,
337
+ .close = dmaengine_pcm_close,
338
+ .hw_params = dmaengine_pcm_hw_params,
339
+ .trigger = dmaengine_pcm_trigger,
340
+ .pointer = dmaengine_pcm_pointer,
341
+ .pcm_construct = dmaengine_pcm_new,
405342 };
406343
407344 static const struct snd_soc_component_driver dmaengine_pcm_component_process = {
408345 .name = SND_DMAENGINE_PCM_DRV_NAME,
409346 .probe_order = SND_SOC_COMP_ORDER_LATE,
410
- .ops = &dmaengine_pcm_process_ops,
411
- .pcm_new = dmaengine_pcm_new,
347
+ .open = dmaengine_pcm_open,
348
+ .close = dmaengine_pcm_close,
349
+ .hw_params = dmaengine_pcm_hw_params,
350
+ .trigger = dmaengine_pcm_trigger,
351
+ .pointer = dmaengine_pcm_pointer,
352
+ .copy_user = dmaengine_copy_user,
353
+ .pcm_construct = dmaengine_pcm_new,
412354 };
413355
414356 static const char * const dmaengine_pcm_dma_channel_names[] = {
....@@ -423,9 +365,8 @@
423365 const char *name;
424366 struct dma_chan *chan;
425367
426
- if ((pcm->flags & (SND_DMAENGINE_PCM_FLAG_NO_DT |
427
- SND_DMAENGINE_PCM_FLAG_CUSTOM_CHANNEL_NAME)) ||
428
- !dev->of_node)
368
+ if ((pcm->flags & SND_DMAENGINE_PCM_FLAG_NO_DT) || (!dev->of_node &&
369
+ !(config && config->dma_dev && config->dma_dev->of_node)))
429370 return 0;
430371
431372 if (config && config->dma_dev) {
....@@ -440,16 +381,20 @@
440381 dev = config->dma_dev;
441382 }
442383
443
- for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
444
- i++) {
384
+ for_each_pcm_streams(i) {
445385 if (pcm->flags & SND_DMAENGINE_PCM_FLAG_HALF_DUPLEX)
446386 name = "rx-tx";
447387 else
448388 name = dmaengine_pcm_dma_channel_names[i];
449389 if (config && config->chan_names[i])
450390 name = config->chan_names[i];
451
- chan = dma_request_slave_channel_reason(dev, name);
391
+ chan = dma_request_chan(dev, name);
452392 if (IS_ERR(chan)) {
393
+ /*
394
+ * Only report probe deferral errors, channels
395
+ * might not be present for devices that
396
+ * support only TX or only RX.
397
+ */
453398 if (PTR_ERR(chan) == -EPROBE_DEFER)
454399 return -EPROBE_DEFER;
455400 pcm->chan[i] = NULL;
....@@ -470,8 +415,7 @@
470415 {
471416 unsigned int i;
472417
473
- for (i = SNDRV_PCM_STREAM_PLAYBACK; i <= SNDRV_PCM_STREAM_CAPTURE;
474
- i++) {
418
+ for_each_pcm_streams(i) {
475419 if (!pcm->chan[i])
476420 continue;
477421 dma_release_channel(pcm->chan[i]);
....@@ -489,6 +433,7 @@
489433 int snd_dmaengine_pcm_register(struct device *dev,
490434 const struct snd_dmaengine_pcm_config *config, unsigned int flags)
491435 {
436
+ const struct snd_soc_component_driver *driver;
492437 struct dmaengine_pcm *pcm;
493438 int ret;
494439
....@@ -507,12 +452,15 @@
507452 goto err_free_dma;
508453
509454 if (config && config->process)
510
- ret = snd_soc_add_component(dev, &pcm->component,
511
- &dmaengine_pcm_component_process,
512
- NULL, 0);
455
+ driver = &dmaengine_pcm_component_process;
513456 else
514
- ret = snd_soc_add_component(dev, &pcm->component,
515
- &dmaengine_pcm_component, NULL, 0);
457
+ driver = &dmaengine_pcm_component;
458
+
459
+ ret = snd_soc_component_initialize(&pcm->component, driver, dev);
460
+ if (ret)
461
+ goto err_free_dma;
462
+
463
+ ret = snd_soc_add_component(&pcm->component, NULL, 0);
516464 if (ret)
517465 goto err_free_dma;
518466
....@@ -543,7 +491,7 @@
543491
544492 pcm = soc_component_to_pcm(component);
545493
546
- snd_soc_unregister_component(dev);
494
+ snd_soc_unregister_component_by_driver(dev, component->driver);
547495 dmaengine_pcm_release_chan(pcm);
548496 kfree(pcm);
549497 }