2 * sound.c - Audio Application Interface Module for Mostcore
4 * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * This file is licensed under GPLv2.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
17 #include <linux/printk.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <linux/sched.h>
24 #include <linux/kthread.h>
27 #define DRIVER_NAME "sound"
29 static struct list_head dev_list;
30 static struct most_aim audio_aim;
33 * struct channel - private structure to keep channel specific data
34 * @substream: stores the substream structure
35 * @iface: interface for which the channel belongs to
36 * @cfg: channel configuration
37 * @card: registered sound card
38 * @list: list for private use
40 * @period_pos: current period position (ring buffer)
41 * @buffer_pos: current buffer position (ring buffer)
42 * @is_stream_running: identifies whether a stream is running or not
43 * @opened: set when the stream is opened
44 * @playback_task: playback thread
45 * @playback_waitq: waitq used by playback thread
48 struct snd_pcm_substream *substream;
49 struct snd_pcm_hardware pcm_hardware;
50 struct most_interface *iface;
51 struct most_channel_config *cfg;
52 struct snd_card *card;
53 struct list_head list;
55 unsigned int period_pos;
56 unsigned int buffer_pos;
57 bool is_stream_running;
59 struct task_struct *playback_task;
60 wait_queue_head_t playback_waitq;
62 void (*copy_fn)(void *alsa, void *most, unsigned int bytes);
65 #define MOST_PCM_INFO (SNDRV_PCM_INFO_MMAP | \
66 SNDRV_PCM_INFO_MMAP_VALID | \
67 SNDRV_PCM_INFO_BATCH | \
68 SNDRV_PCM_INFO_INTERLEAVED | \
69 SNDRV_PCM_INFO_BLOCK_TRANSFER)
71 #define swap16(val) ( \
72 (((u16)(val) << 8) & (u16)0xFF00) | \
73 (((u16)(val) >> 8) & (u16)0x00FF))
75 #define swap32(val) ( \
76 (((u32)(val) << 24) & (u32)0xFF000000) | \
77 (((u32)(val) << 8) & (u32)0x00FF0000) | \
78 (((u32)(val) >> 8) & (u32)0x0000FF00) | \
79 (((u32)(val) >> 24) & (u32)0x000000FF))
81 static void swap_copy16(u16 *dest, const u16 *source, unsigned int bytes)
85 while (i < (bytes / 2)) {
86 dest[i] = swap16(source[i]);
91 static void swap_copy24(u8 *dest, const u8 *source, unsigned int bytes)
97 while (i < bytes - 2) {
98 dest[i] = source[i + 2];
99 dest[i + 1] = source[i + 1];
100 dest[i + 2] = source[i];
105 static void swap_copy32(u32 *dest, const u32 *source, unsigned int bytes)
109 while (i < bytes / 4) {
110 dest[i] = swap32(source[i]);
115 static void alsa_to_most_memcpy(void *alsa, void *most, unsigned int bytes)
117 memcpy(most, alsa, bytes);
120 static void alsa_to_most_copy16(void *alsa, void *most, unsigned int bytes)
122 swap_copy16(most, alsa, bytes);
125 static void alsa_to_most_copy24(void *alsa, void *most, unsigned int bytes)
127 swap_copy24(most, alsa, bytes);
130 static void alsa_to_most_copy32(void *alsa, void *most, unsigned int bytes)
132 swap_copy32(most, alsa, bytes);
135 static void most_to_alsa_memcpy(void *alsa, void *most, unsigned int bytes)
137 memcpy(alsa, most, bytes);
140 static void most_to_alsa_copy16(void *alsa, void *most, unsigned int bytes)
142 swap_copy16(alsa, most, bytes);
145 static void most_to_alsa_copy24(void *alsa, void *most, unsigned int bytes)
147 swap_copy24(alsa, most, bytes);
150 static void most_to_alsa_copy32(void *alsa, void *most, unsigned int bytes)
152 swap_copy32(alsa, most, bytes);
156 * get_channel - get pointer to channel
157 * @iface: interface structure
158 * @channel_id: channel ID
160 * This traverses the channel list and returns the channel matching the
163 * Returns pointer to channel on success or NULL otherwise.
165 static struct channel *get_channel(struct most_interface *iface,
168 struct channel *channel, *tmp;
170 list_for_each_entry_safe(channel, tmp, &dev_list, list) {
171 if ((channel->iface == iface) && (channel->id == channel_id))
179 * copy_data - implements data copying function
181 * @mbo: MBO from core
183 * Copy data from/to ring buffer to/from MBO and update the buffer position
185 static bool copy_data(struct channel *channel, struct mbo *mbo)
187 struct snd_pcm_runtime *const runtime = channel->substream->runtime;
188 unsigned int const frame_bytes = channel->cfg->subbuffer_size;
189 unsigned int const buffer_size = runtime->buffer_size;
193 if (channel->cfg->direction & MOST_CH_RX)
194 frames = mbo->processed_length / frame_bytes;
196 frames = mbo->buffer_length / frame_bytes;
197 fr0 = min(buffer_size - channel->buffer_pos, frames);
199 channel->copy_fn(runtime->dma_area + channel->buffer_pos * frame_bytes,
204 /* wrap around at end of ring buffer */
205 channel->copy_fn(runtime->dma_area,
206 mbo->virt_address + fr0 * frame_bytes,
207 (frames - fr0) * frame_bytes);
210 channel->buffer_pos += frames;
211 if (channel->buffer_pos >= buffer_size)
212 channel->buffer_pos -= buffer_size;
213 channel->period_pos += frames;
214 if (channel->period_pos >= runtime->period_size) {
215 channel->period_pos -= runtime->period_size;
223 * playback_thread - function implements the playback thread
224 * @data: private data
226 * Thread which does the playback functionality in a loop. It waits for a free
227 * MBO from mostcore for a particular channel and copy the data from ring buffer
228 * to MBO. Submit the MBO back to mostcore, after copying the data.
230 * Returns 0 on success or error code otherwise.
232 static int playback_thread(void *data)
234 struct channel *const channel = data;
236 while (!kthread_should_stop()) {
237 struct mbo *mbo = NULL;
238 bool period_elapsed = false;
241 wait_event_interruptible(
242 channel->playback_waitq,
243 kthread_should_stop() ||
244 (channel->is_stream_running &&
245 (mbo = most_get_mbo(channel->iface, channel->id,
250 if (channel->is_stream_running)
251 period_elapsed = copy_data(channel, mbo);
253 memset(mbo->virt_address, 0, mbo->buffer_length);
255 ret = most_submit_mbo(mbo);
257 channel->is_stream_running = false;
260 snd_pcm_period_elapsed(channel->substream);
267 * pcm_open - implements open callback function for PCM middle layer
268 * @substream: pointer to ALSA PCM substream
270 * This is called when a PCM substream is opened. At least, the function should
271 * initialize the runtime->hw record.
273 * Returns 0 on success or error code otherwise.
275 static int pcm_open(struct snd_pcm_substream *substream)
277 struct channel *channel = substream->private_data;
278 struct snd_pcm_runtime *runtime = substream->runtime;
279 struct most_channel_config *cfg = channel->cfg;
281 channel->substream = substream;
283 if (cfg->direction == MOST_CH_TX) {
284 channel->playback_task = kthread_run(playback_thread, channel,
285 "most_audio_playback");
286 if (IS_ERR(channel->playback_task)) {
287 pr_err("Couldn't start thread\n");
288 return PTR_ERR(channel->playback_task);
292 if (most_start_channel(channel->iface, channel->id, &audio_aim)) {
293 pr_err("most_start_channel() failed!\n");
294 if (cfg->direction == MOST_CH_TX)
295 kthread_stop(channel->playback_task);
299 runtime->hw = channel->pcm_hardware;
304 * pcm_close - implements close callback function for PCM middle layer
305 * @substream: sub-stream pointer
307 * Obviously, this is called when a PCM substream is closed. Any private
308 * instance for a PCM substream allocated in the open callback will be
311 * Returns 0 on success or error code otherwise.
313 static int pcm_close(struct snd_pcm_substream *substream)
315 struct channel *channel = substream->private_data;
317 if (channel->cfg->direction == MOST_CH_TX)
318 kthread_stop(channel->playback_task);
319 most_stop_channel(channel->iface, channel->id, &audio_aim);
325 * pcm_hw_params - implements hw_params callback function for PCM middle layer
326 * @substream: sub-stream pointer
327 * @hw_params: contains the hardware parameters set by the application
329 * This is called when the hardware parameters is set by the application, that
330 * is, once when the buffer size, the period size, the format, etc. are defined
331 * for the PCM substream. Many hardware setups should be done is this callback,
332 * including the allocation of buffers.
334 * Returns 0 on success or error code otherwise.
336 static int pcm_hw_params(struct snd_pcm_substream *substream,
337 struct snd_pcm_hw_params *hw_params)
339 struct channel *channel = substream->private_data;
341 if ((params_channels(hw_params) > channel->pcm_hardware.channels_max) ||
342 (params_channels(hw_params) < channel->pcm_hardware.channels_min)) {
343 pr_err("Requested number of channels not supported.\n");
346 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
347 params_buffer_bytes(hw_params));
351 * pcm_hw_free - implements hw_free callback function for PCM middle layer
352 * @substream: substream pointer
354 * This is called to release the resources allocated via hw_params.
355 * This function will be always called before the close callback is called.
357 * Returns 0 on success or error code otherwise.
359 static int pcm_hw_free(struct snd_pcm_substream *substream)
361 return snd_pcm_lib_free_vmalloc_buffer(substream);
365 * pcm_prepare - implements prepare callback function for PCM middle layer
366 * @substream: substream pointer
368 * This callback is called when the PCM is "prepared". Format rate, sample rate,
369 * etc., can be set here. This callback can be called many times at each setup.
371 * Returns 0 on success or error code otherwise.
373 static int pcm_prepare(struct snd_pcm_substream *substream)
375 struct channel *channel = substream->private_data;
376 struct snd_pcm_runtime *runtime = substream->runtime;
377 struct most_channel_config *cfg = channel->cfg;
378 int width = snd_pcm_format_physical_width(runtime->format);
380 channel->copy_fn = NULL;
382 if (cfg->direction == MOST_CH_TX) {
383 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
384 channel->copy_fn = alsa_to_most_memcpy;
385 else if (width == 16)
386 channel->copy_fn = alsa_to_most_copy16;
387 else if (width == 24)
388 channel->copy_fn = alsa_to_most_copy24;
389 else if (width == 32)
390 channel->copy_fn = alsa_to_most_copy32;
392 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
393 channel->copy_fn = most_to_alsa_memcpy;
394 else if (width == 16)
395 channel->copy_fn = most_to_alsa_copy16;
396 else if (width == 24)
397 channel->copy_fn = most_to_alsa_copy24;
398 else if (width == 32)
399 channel->copy_fn = most_to_alsa_copy32;
402 if (!channel->copy_fn) {
403 pr_err("unsupported format\n");
407 channel->period_pos = 0;
408 channel->buffer_pos = 0;
414 * pcm_trigger - implements trigger callback function for PCM middle layer
415 * @substream: substream pointer
416 * @cmd: action to perform
418 * This is called when the PCM is started, stopped or paused. The action will be
419 * specified in the second argument, SNDRV_PCM_TRIGGER_XXX
421 * Returns 0 on success or error code otherwise.
423 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
425 struct channel *channel = substream->private_data;
428 case SNDRV_PCM_TRIGGER_START:
429 channel->is_stream_running = true;
430 wake_up_interruptible(&channel->playback_waitq);
433 case SNDRV_PCM_TRIGGER_STOP:
434 channel->is_stream_running = false;
438 pr_info("pcm_trigger(), invalid\n");
445 * pcm_pointer - implements pointer callback function for PCM middle layer
446 * @substream: substream pointer
448 * This callback is called when the PCM middle layer inquires the current
449 * hardware position on the buffer. The position must be returned in frames,
450 * ranging from 0 to buffer_size-1.
452 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
454 struct channel *channel = substream->private_data;
456 return channel->buffer_pos;
460 * Initialization of struct snd_pcm_ops
462 static struct snd_pcm_ops pcm_ops = {
465 .ioctl = snd_pcm_lib_ioctl,
466 .hw_params = pcm_hw_params,
467 .hw_free = pcm_hw_free,
468 .prepare = pcm_prepare,
469 .trigger = pcm_trigger,
470 .pointer = pcm_pointer,
471 .page = snd_pcm_lib_get_vmalloc_page,
472 .mmap = snd_pcm_lib_mmap_vmalloc,
475 static int split_arg_list(char *buf, char **card_name, char **pcm_format)
477 *card_name = strsep(&buf, ".");
480 *pcm_format = strsep(&buf, ".\n");
486 static int audio_set_hw_params(struct snd_pcm_hardware *pcm_hw,
488 struct most_channel_config *cfg)
490 pcm_hw->info = MOST_PCM_INFO;
491 pcm_hw->rates = SNDRV_PCM_RATE_48000;
492 pcm_hw->rate_min = 48000;
493 pcm_hw->rate_max = 48000;
494 pcm_hw->buffer_bytes_max = cfg->num_buffers * cfg->buffer_size;
495 pcm_hw->period_bytes_min = cfg->buffer_size;
496 pcm_hw->period_bytes_max = cfg->buffer_size;
497 pcm_hw->periods_min = 1;
498 pcm_hw->periods_max = cfg->num_buffers;
500 if (!strcmp(pcm_format, "1x8")) {
501 if (cfg->subbuffer_size != 1)
503 pr_info("PCM format is 8-bit mono\n");
504 pcm_hw->channels_min = 1;
505 pcm_hw->channels_max = 1;
506 pcm_hw->formats = SNDRV_PCM_FMTBIT_S8;
507 } else if (!strcmp(pcm_format, "2x16")) {
508 if (cfg->subbuffer_size != 4)
510 pr_info("PCM format is 16-bit stereo\n");
511 pcm_hw->channels_min = 2;
512 pcm_hw->channels_max = 2;
513 pcm_hw->formats = SNDRV_PCM_FMTBIT_S16_LE |
514 SNDRV_PCM_FMTBIT_S16_BE;
515 } else if (!strcmp(pcm_format, "2x24")) {
516 if (cfg->subbuffer_size != 6)
518 pr_info("PCM format is 24-bit stereo\n");
519 pcm_hw->channels_min = 2;
520 pcm_hw->channels_max = 2;
521 pcm_hw->formats = SNDRV_PCM_FMTBIT_S24_3LE |
522 SNDRV_PCM_FMTBIT_S24_3BE;
523 } else if (!strcmp(pcm_format, "2x32")) {
524 if (cfg->subbuffer_size != 8)
526 pr_info("PCM format is 32-bit stereo\n");
527 pcm_hw->channels_min = 2;
528 pcm_hw->channels_max = 2;
529 pcm_hw->formats = SNDRV_PCM_FMTBIT_S32_LE |
530 SNDRV_PCM_FMTBIT_S32_BE;
531 } else if (!strcmp(pcm_format, "6x16")) {
532 if (cfg->subbuffer_size != 12)
534 pr_info("PCM format is 16-bit 5.1 multi channel\n");
535 pcm_hw->channels_min = 6;
536 pcm_hw->channels_max = 6;
537 pcm_hw->formats = SNDRV_PCM_FMTBIT_S16_LE |
538 SNDRV_PCM_FMTBIT_S16_BE;
540 pr_err("PCM format %s not supported\n", pcm_format);
545 pr_err("Audio resolution doesn't fit subbuffer size\n");
550 * audio_probe_channel - probe function of the driver module
551 * @iface: pointer to interface instance
552 * @channel_id: channel index/ID
553 * @cfg: pointer to actual channel configuration
554 * @parent: pointer to kobject (needed for sysfs hook-up)
555 * @arg_list: string that provides the name of the device to be created in /dev
556 * plus the desired audio resolution
558 * Creates sound card, pcm device, sets pcm ops and registers sound card.
560 * Returns 0 on success or error code otherwise.
562 static int audio_probe_channel(struct most_interface *iface, int channel_id,
563 struct most_channel_config *cfg,
564 struct kobject *parent, char *arg_list)
566 struct channel *channel;
567 struct snd_card *card;
569 int playback_count = 0;
570 int capture_count = 0;
579 if (cfg->data_type != MOST_CH_SYNC) {
580 pr_err("Incompatible channel type\n");
584 if (get_channel(iface, channel_id)) {
585 pr_err("channel (%s:%d) is already linked\n",
586 iface->description, channel_id);
590 if (cfg->direction == MOST_CH_TX) {
592 direction = SNDRV_PCM_STREAM_PLAYBACK;
595 direction = SNDRV_PCM_STREAM_CAPTURE;
598 ret = split_arg_list(arg_list, &card_name, &pcm_format);
600 pr_info("PCM format missing\n");
604 ret = snd_card_new(NULL, -1, card_name, THIS_MODULE,
605 sizeof(*channel), &card);
609 channel = card->private_data;
610 channel->card = card;
612 channel->iface = iface;
613 channel->id = channel_id;
614 init_waitqueue_head(&channel->playback_waitq);
616 if (audio_set_hw_params(&channel->pcm_hardware, pcm_format, cfg))
619 snprintf(card->driver, sizeof(card->driver), "%s", DRIVER_NAME);
620 snprintf(card->shortname, sizeof(card->shortname), "Microchip MOST:%d",
622 snprintf(card->longname, sizeof(card->longname), "%s at %s, ch %d",
623 card->shortname, iface->description, channel_id);
625 ret = snd_pcm_new(card, card_name, 0, playback_count,
626 capture_count, &pcm);
630 pcm->private_data = channel;
632 snd_pcm_set_ops(pcm, direction, &pcm_ops);
634 ret = snd_card_register(card);
638 list_add_tail(&channel->list, &dev_list);
648 * audio_disconnect_channel - function to disconnect a channel
649 * @iface: pointer to interface instance
650 * @channel_id: channel index
652 * This frees allocated memory and removes the sound card from ALSA
654 * Returns 0 on success or error code otherwise.
656 static int audio_disconnect_channel(struct most_interface *iface,
659 struct channel *channel;
661 channel = get_channel(iface, channel_id);
663 pr_err("sound_disconnect_channel(), invalid channel %d\n",
668 list_del(&channel->list);
669 snd_card_free(channel->card);
675 * audio_rx_completion - completion handler for rx channels
676 * @mbo: pointer to buffer object that has completed
678 * This searches for the channel this MBO belongs to and copy the data from MBO
681 * Returns 0 on success or error code otherwise.
683 static int audio_rx_completion(struct mbo *mbo)
685 struct channel *channel = get_channel(mbo->ifp, mbo->hdm_channel_id);
686 bool period_elapsed = false;
689 pr_err("sound_rx_completion(), invalid channel %d\n",
690 mbo->hdm_channel_id);
694 if (channel->is_stream_running)
695 period_elapsed = copy_data(channel, mbo);
700 snd_pcm_period_elapsed(channel->substream);
706 * audio_tx_completion - completion handler for tx channels
707 * @iface: pointer to interface instance
708 * @channel_id: channel index/ID
710 * This searches the channel that belongs to this combination of interface
711 * pointer and channel ID and wakes a process sitting in the wait queue of
714 * Returns 0 on success or error code otherwise.
716 static int audio_tx_completion(struct most_interface *iface, int channel_id)
718 struct channel *channel = get_channel(iface, channel_id);
721 pr_err("sound_tx_completion(), invalid channel %d\n",
726 wake_up_interruptible(&channel->playback_waitq);
732 * Initialization of the struct most_aim
734 static struct most_aim audio_aim = {
736 .probe_channel = audio_probe_channel,
737 .disconnect_channel = audio_disconnect_channel,
738 .rx_completion = audio_rx_completion,
739 .tx_completion = audio_tx_completion,
742 static int __init audio_init(void)
746 INIT_LIST_HEAD(&dev_list);
748 return most_register_aim(&audio_aim);
751 static void __exit audio_exit(void)
753 struct channel *channel, *tmp;
757 list_for_each_entry_safe(channel, tmp, &dev_list, list) {
758 list_del(&channel->list);
759 snd_card_free(channel->card);
762 most_deregister_aim(&audio_aim);
765 module_init(audio_init);
766 module_exit(audio_exit);
768 MODULE_LICENSE("GPL");
769 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
770 MODULE_DESCRIPTION("Audio Application Interface Module for MostCore");