GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / staging / vc04_services / bcm2835-audio / bcm2835-pcm.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011 Broadcom Corporation.  All rights reserved. */
3
4 #include <linux/interrupt.h>
5 #include <linux/slab.h>
6
7 #include <sound/asoundef.h>
8
9 #include "bcm2835.h"
10
11 /* hardware definition */
12 static const struct snd_pcm_hardware snd_bcm2835_playback_hw = {
13         .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
14         SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
15         .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
16         .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
17         .rate_min = 8000,
18         .rate_max = 48000,
19         .channels_min = 1,
20         .channels_max = 2,
21         .buffer_bytes_max = 128 * 1024,
22         .period_bytes_min = 1 * 1024,
23         .period_bytes_max = 128 * 1024,
24         .periods_min = 1,
25         .periods_max = 128,
26 };
27
28 static const struct snd_pcm_hardware snd_bcm2835_playback_spdif_hw = {
29         .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
30         SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
31         .formats = SNDRV_PCM_FMTBIT_S16_LE,
32         .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_44100 |
33         SNDRV_PCM_RATE_48000,
34         .rate_min = 44100,
35         .rate_max = 48000,
36         .channels_min = 2,
37         .channels_max = 2,
38         .buffer_bytes_max = 128 * 1024,
39         .period_bytes_min = 1 * 1024,
40         .period_bytes_max = 128 * 1024,
41         .periods_min = 1,
42         .periods_max = 128,
43 };
44
45 static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
46 {
47         audio_info("Freeing up alsa stream here ..\n");
48         kfree(runtime->private_data);
49         runtime->private_data = NULL;
50 }
51
52 void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream)
53 {
54         unsigned int consumed = 0;
55         int new_period = 0;
56
57         audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
58                 alsa_stream ? alsa_stream->substream : 0);
59
60         if (alsa_stream->open)
61                 consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
62
63         /* We get called only if playback was triggered, So, the number of buffers we retrieve in
64          * each iteration are the buffers that have been played out already
65          */
66
67         if (alsa_stream->period_size) {
68                 if ((alsa_stream->pos / alsa_stream->period_size) !=
69                         ((alsa_stream->pos + consumed) / alsa_stream->period_size))
70                         new_period = 1;
71         }
72         audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
73                 alsa_stream->pos,
74                 consumed,
75                 alsa_stream->buffer_size,
76                 (int) (alsa_stream->period_size * alsa_stream->substream->runtime->periods),
77                 frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
78                 new_period);
79         if (alsa_stream->buffer_size) {
80                 alsa_stream->pos += consumed & ~(1 << 30);
81                 alsa_stream->pos %= alsa_stream->buffer_size;
82         }
83
84         if (alsa_stream->substream) {
85                 if (new_period)
86                         snd_pcm_period_elapsed(alsa_stream->substream);
87         } else {
88                 audio_warning(" unexpected NULL substream\n");
89         }
90 }
91
92 /* open callback */
93 static int snd_bcm2835_playback_open_generic(
94         struct snd_pcm_substream *substream, int spdif)
95 {
96         struct bcm2835_chip *chip = snd_pcm_substream_chip(substream);
97         struct snd_pcm_runtime *runtime = substream->runtime;
98         struct bcm2835_alsa_stream *alsa_stream;
99         int idx;
100         int err;
101
102         if (mutex_lock_interruptible(&chip->audio_mutex)) {
103                 audio_error("Interrupted whilst waiting for lock\n");
104                 return -EINTR;
105         }
106         audio_info("Alsa open (%d)\n", substream->number);
107         idx = substream->number;
108
109         if (spdif && chip->opened) {
110                 err = -EBUSY;
111                 goto out;
112         } else if (!spdif && (chip->opened & (1 << idx))) {
113                 err = -EBUSY;
114                 goto out;
115         }
116         if (idx >= MAX_SUBSTREAMS) {
117                 audio_error
118                         ("substream(%d) device doesn't exist max(%d) substreams allowed\n",
119                         idx, MAX_SUBSTREAMS);
120                 err = -ENODEV;
121                 goto out;
122         }
123
124         /* Check if we are ready */
125         if (!(chip->avail_substreams & (1 << idx))) {
126                 /* We are not ready yet */
127                 audio_error("substream(%d) device is not ready yet\n", idx);
128                 err = -EAGAIN;
129                 goto out;
130         }
131
132         alsa_stream = kzalloc(sizeof(*alsa_stream), GFP_KERNEL);
133         if (!alsa_stream) {
134                 err = -ENOMEM;
135                 goto out;
136         }
137
138         /* Initialise alsa_stream */
139         alsa_stream->chip = chip;
140         alsa_stream->substream = substream;
141         alsa_stream->idx = idx;
142
143         spin_lock_init(&alsa_stream->lock);
144
145         err = bcm2835_audio_open(alsa_stream);
146         if (err) {
147                 kfree(alsa_stream);
148                 goto out;
149         }
150         runtime->private_data = alsa_stream;
151         runtime->private_free = snd_bcm2835_playback_free;
152         if (spdif) {
153                 runtime->hw = snd_bcm2835_playback_spdif_hw;
154         } else {
155                 /* clear spdif status, as we are not in spdif mode */
156                 chip->spdif_status = 0;
157                 runtime->hw = snd_bcm2835_playback_hw;
158         }
159         /* minimum 16 bytes alignment (for vchiq bulk transfers) */
160         snd_pcm_hw_constraint_step(runtime,
161                                    0,
162                                    SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
163                                    16);
164
165         chip->alsa_stream[idx] = alsa_stream;
166
167         chip->opened |= (1 << idx);
168         alsa_stream->open = 1;
169         alsa_stream->draining = 1;
170
171 out:
172         mutex_unlock(&chip->audio_mutex);
173
174         return err;
175 }
176
177 static int snd_bcm2835_playback_open(struct snd_pcm_substream *substream)
178 {
179         return snd_bcm2835_playback_open_generic(substream, 0);
180 }
181
182 static int snd_bcm2835_playback_spdif_open(struct snd_pcm_substream *substream)
183 {
184         return snd_bcm2835_playback_open_generic(substream, 1);
185 }
186
187 /* close callback */
188 static int snd_bcm2835_playback_close(struct snd_pcm_substream *substream)
189 {
190         /* the hardware-specific codes will be here */
191
192         struct bcm2835_chip *chip;
193         struct snd_pcm_runtime *runtime;
194         struct bcm2835_alsa_stream *alsa_stream;
195
196         chip = snd_pcm_substream_chip(substream);
197         if (mutex_lock_interruptible(&chip->audio_mutex)) {
198                 audio_error("Interrupted whilst waiting for lock\n");
199                 return -EINTR;
200         }
201         runtime = substream->runtime;
202         alsa_stream = runtime->private_data;
203
204         audio_info("Alsa close\n");
205
206         /*
207          * Call stop if it's still running. This happens when app
208          * is force killed and we don't get a stop trigger.
209          */
210         if (alsa_stream->running) {
211                 int err;
212
213                 err = bcm2835_audio_stop(alsa_stream);
214                 alsa_stream->running = 0;
215                 if (err)
216                         audio_error(" Failed to STOP alsa device\n");
217         }
218
219         alsa_stream->period_size = 0;
220         alsa_stream->buffer_size = 0;
221
222         if (alsa_stream->open) {
223                 alsa_stream->open = 0;
224                 bcm2835_audio_close(alsa_stream);
225         }
226         if (alsa_stream->chip)
227                 alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
228         /*
229          * Do not free up alsa_stream here, it will be freed up by
230          * runtime->private_free callback we registered in *_open above
231          */
232
233         chip->opened &= ~(1 << substream->number);
234
235         mutex_unlock(&chip->audio_mutex);
236
237         return 0;
238 }
239
240 /* hw_params callback */
241 static int snd_bcm2835_pcm_hw_params(struct snd_pcm_substream *substream,
242         struct snd_pcm_hw_params *params)
243 {
244         struct snd_pcm_runtime *runtime = substream->runtime;
245         struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
246         int err;
247
248         err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
249         if (err < 0) {
250                 audio_error
251                         (" pcm_lib_malloc failed to allocated pages for buffers\n");
252                 return err;
253         }
254
255         alsa_stream->channels = params_channels(params);
256         alsa_stream->params_rate = params_rate(params);
257         alsa_stream->pcm_format_width = snd_pcm_format_width(params_format(params));
258
259         return err;
260 }
261
262 /* hw_free callback */
263 static int snd_bcm2835_pcm_hw_free(struct snd_pcm_substream *substream)
264 {
265         return snd_pcm_lib_free_pages(substream);
266 }
267
268 /* prepare callback */
269 static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
270 {
271         struct bcm2835_chip *chip = snd_pcm_substream_chip(substream);
272         struct snd_pcm_runtime *runtime = substream->runtime;
273         struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
274         int channels;
275         int err;
276
277         if (mutex_lock_interruptible(&chip->audio_mutex))
278                 return -EINTR;
279
280         /* notify the vchiq that it should enter spdif passthrough mode by
281          * setting channels=0 (see
282          * https://github.com/raspberrypi/linux/issues/528)
283          */
284         if (chip->spdif_status & IEC958_AES0_NONAUDIO)
285                 channels = 0;
286         else
287                 channels = alsa_stream->channels;
288
289         err = bcm2835_audio_set_params(alsa_stream, channels,
290                 alsa_stream->params_rate,
291                 alsa_stream->pcm_format_width);
292         if (err < 0)
293                 audio_error(" error setting hw params\n");
294
295         bcm2835_audio_setup(alsa_stream);
296
297         /* in preparation of the stream, set the controls (volume level) of the stream */
298         bcm2835_audio_set_ctls(alsa_stream->chip);
299
300         memset(&alsa_stream->pcm_indirect, 0, sizeof(alsa_stream->pcm_indirect));
301
302         alsa_stream->pcm_indirect.hw_buffer_size =
303                 alsa_stream->pcm_indirect.sw_buffer_size =
304                 snd_pcm_lib_buffer_bytes(substream);
305
306         alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
307         alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
308         alsa_stream->pos = 0;
309
310         audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
311                 alsa_stream->buffer_size, alsa_stream->period_size,
312                 alsa_stream->pos, runtime->frame_bits);
313
314         mutex_unlock(&chip->audio_mutex);
315         return 0;
316 }
317
318 static void snd_bcm2835_pcm_transfer(struct snd_pcm_substream *substream,
319         struct snd_pcm_indirect *rec, size_t bytes)
320 {
321         struct snd_pcm_runtime *runtime = substream->runtime;
322         struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
323         void *src = (void *) (substream->runtime->dma_area + rec->sw_data);
324         int err;
325
326         err = bcm2835_audio_write(alsa_stream, bytes, src);
327         if (err)
328                 audio_error(" Failed to transfer to alsa device (%d)\n", err);
329
330 }
331
332 static int snd_bcm2835_pcm_ack(struct snd_pcm_substream *substream)
333 {
334         struct snd_pcm_runtime *runtime = substream->runtime;
335         struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
336         struct snd_pcm_indirect *pcm_indirect = &alsa_stream->pcm_indirect;
337
338         pcm_indirect->hw_queue_size = runtime->hw.buffer_bytes_max;
339         return snd_pcm_indirect_playback_transfer(substream, pcm_indirect,
340                                                   snd_bcm2835_pcm_transfer);
341 }
342
343 /* trigger callback */
344 static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
345 {
346         struct snd_pcm_runtime *runtime = substream->runtime;
347         struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
348         int err = 0;
349
350         switch (cmd) {
351         case SNDRV_PCM_TRIGGER_START:
352                 audio_debug("bcm2835_AUDIO_TRIGGER_START running=%d\n",
353                         alsa_stream->running);
354                 if (!alsa_stream->running) {
355                         err = bcm2835_audio_start(alsa_stream);
356                         if (!err) {
357                                 alsa_stream->pcm_indirect.hw_io =
358                                         alsa_stream->pcm_indirect.hw_data =
359                                         bytes_to_frames(runtime,
360                                         alsa_stream->pos);
361                                 substream->ops->ack(substream);
362                                 alsa_stream->running = 1;
363                                 alsa_stream->draining = 1;
364                         } else {
365                                 audio_error(" Failed to START alsa device (%d)\n", err);
366                         }
367                 }
368                 break;
369         case SNDRV_PCM_TRIGGER_STOP:
370                 audio_debug
371                         ("bcm2835_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
372                         alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);
373                 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
374                         audio_info("DRAINING\n");
375                         alsa_stream->draining = 1;
376                 } else {
377                         audio_info("DROPPING\n");
378                         alsa_stream->draining = 0;
379                 }
380                 if (alsa_stream->running) {
381                         err = bcm2835_audio_stop(alsa_stream);
382                         if (err != 0)
383                                 audio_error(" Failed to STOP alsa device (%d)\n", err);
384                         alsa_stream->running = 0;
385                 }
386                 break;
387         default:
388                 err = -EINVAL;
389         }
390
391         return err;
392 }
393
394 /* pointer callback */
395 static snd_pcm_uframes_t
396 snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
397 {
398         struct snd_pcm_runtime *runtime = substream->runtime;
399         struct bcm2835_alsa_stream *alsa_stream = runtime->private_data;
400
401         audio_debug("pcm_pointer... (%d) hwptr=%d appl=%d pos=%d\n", 0,
402                 frames_to_bytes(runtime, runtime->status->hw_ptr),
403                 frames_to_bytes(runtime, runtime->control->appl_ptr),
404                 alsa_stream->pos);
405
406         return snd_pcm_indirect_playback_pointer(substream,
407                 &alsa_stream->pcm_indirect,
408                 alsa_stream->pos);
409 }
410
411 static int snd_bcm2835_pcm_lib_ioctl(struct snd_pcm_substream *substream,
412         unsigned int cmd, void *arg)
413 {
414         int ret = snd_pcm_lib_ioctl(substream, cmd, arg);
415
416         audio_info(" .. substream=%p, cmd=%d, arg=%p (%x) ret=%d\n", substream,
417                 cmd, arg, arg ? *(unsigned int *)arg : 0, ret);
418         return ret;
419 }
420
421 /* operators */
422 static const struct snd_pcm_ops snd_bcm2835_playback_ops = {
423         .open = snd_bcm2835_playback_open,
424         .close = snd_bcm2835_playback_close,
425         .ioctl = snd_bcm2835_pcm_lib_ioctl,
426         .hw_params = snd_bcm2835_pcm_hw_params,
427         .hw_free = snd_bcm2835_pcm_hw_free,
428         .prepare = snd_bcm2835_pcm_prepare,
429         .trigger = snd_bcm2835_pcm_trigger,
430         .pointer = snd_bcm2835_pcm_pointer,
431         .ack = snd_bcm2835_pcm_ack,
432 };
433
434 static const struct snd_pcm_ops snd_bcm2835_playback_spdif_ops = {
435         .open = snd_bcm2835_playback_spdif_open,
436         .close = snd_bcm2835_playback_close,
437         .ioctl = snd_bcm2835_pcm_lib_ioctl,
438         .hw_params = snd_bcm2835_pcm_hw_params,
439         .hw_free = snd_bcm2835_pcm_hw_free,
440         .prepare = snd_bcm2835_pcm_prepare,
441         .trigger = snd_bcm2835_pcm_trigger,
442         .pointer = snd_bcm2835_pcm_pointer,
443         .ack = snd_bcm2835_pcm_ack,
444 };
445
446 /* create a pcm device */
447 int snd_bcm2835_new_pcm(struct bcm2835_chip *chip, u32 numchannels)
448 {
449         struct snd_pcm *pcm;
450         int err;
451
452         mutex_init(&chip->audio_mutex);
453         if (mutex_lock_interruptible(&chip->audio_mutex)) {
454                 audio_error("Interrupted whilst waiting for lock\n");
455                 return -EINTR;
456         }
457         err = snd_pcm_new(chip->card, "bcm2835 ALSA", 0, numchannels, 0, &pcm);
458         if (err < 0)
459                 goto out;
460         pcm->private_data = chip;
461         strcpy(pcm->name, "bcm2835 ALSA");
462         chip->pcm = pcm;
463         chip->dest = AUDIO_DEST_AUTO;
464         chip->volume = alsa2chip(0);
465         chip->mute = CTRL_VOL_UNMUTE; /*disable mute on startup */
466         /* set operators */
467         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
468                         &snd_bcm2835_playback_ops);
469
470         /* pre-allocation of buffers */
471         /* NOTE: this may fail */
472         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
473                                               snd_dma_continuous_data(GFP_KERNEL),
474                                               snd_bcm2835_playback_hw.buffer_bytes_max,
475                                               snd_bcm2835_playback_hw.buffer_bytes_max);
476
477 out:
478         mutex_unlock(&chip->audio_mutex);
479
480         return 0;
481 }
482
483 int snd_bcm2835_new_spdif_pcm(struct bcm2835_chip *chip)
484 {
485         struct snd_pcm *pcm;
486         int err;
487
488         if (mutex_lock_interruptible(&chip->audio_mutex)) {
489                 audio_error("Interrupted whilst waiting for lock\n");
490                 return -EINTR;
491         }
492         err = snd_pcm_new(chip->card, "bcm2835 ALSA", 1, 1, 0, &pcm);
493         if (err < 0)
494                 goto out;
495
496         pcm->private_data = chip;
497         strcpy(pcm->name, "bcm2835 IEC958/HDMI");
498         chip->pcm_spdif = pcm;
499         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
500                         &snd_bcm2835_playback_spdif_ops);
501
502         /* pre-allocation of buffers */
503         /* NOTE: this may fail */
504         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
505                 snd_dma_continuous_data(GFP_KERNEL),
506                 snd_bcm2835_playback_spdif_hw.buffer_bytes_max, snd_bcm2835_playback_spdif_hw.buffer_bytes_max);
507 out:
508         mutex_unlock(&chip->audio_mutex);
509
510         return 0;
511 }
512
513 int snd_bcm2835_new_simple_pcm(struct bcm2835_chip *chip,
514                                const char *name,
515                                enum snd_bcm2835_route route,
516                                u32 numchannels)
517 {
518         struct snd_pcm *pcm;
519         int err;
520
521         mutex_init(&chip->audio_mutex);
522
523         err = snd_pcm_new(chip->card, name, 0, numchannels,
524                           0, &pcm);
525         if (err)
526                 return err;
527
528         pcm->private_data = chip;
529         strcpy(pcm->name, name);
530         chip->pcm = pcm;
531         chip->dest = route;
532         chip->volume = alsa2chip(0);
533         chip->mute = CTRL_VOL_UNMUTE;
534
535         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
536                         &snd_bcm2835_playback_ops);
537
538         snd_pcm_lib_preallocate_pages_for_all(
539                 pcm,
540                 SNDRV_DMA_TYPE_CONTINUOUS,
541                 snd_dma_continuous_data(GFP_KERNEL),
542                 snd_bcm2835_playback_hw.buffer_bytes_max,
543                 snd_bcm2835_playback_hw.buffer_bytes_max);
544
545         return 0;
546 }
547