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;
240 wait_event_interruptible(
241 channel->playback_waitq,
242 kthread_should_stop() ||
243 (channel->is_stream_running &&
244 (mbo = most_get_mbo(channel->iface, channel->id,
249 if (channel->is_stream_running)
250 period_elapsed = copy_data(channel, mbo);
252 memset(mbo->virt_address, 0, mbo->buffer_length);
254 most_submit_mbo(mbo);
256 snd_pcm_period_elapsed(channel->substream);
263 * pcm_open - implements open callback function for PCM middle layer
264 * @substream: pointer to ALSA PCM substream
266 * This is called when a PCM substream is opened. At least, the function should
267 * initialize the runtime->hw record.
269 * Returns 0 on success or error code otherwise.
271 static int pcm_open(struct snd_pcm_substream *substream)
273 struct channel *channel = substream->private_data;
274 struct snd_pcm_runtime *runtime = substream->runtime;
275 struct most_channel_config *cfg = channel->cfg;
277 channel->substream = substream;
279 if (cfg->direction == MOST_CH_TX) {
280 channel->playback_task = kthread_run(playback_thread, channel,
281 "most_audio_playback");
282 if (IS_ERR(channel->playback_task)) {
283 pr_err("Couldn't start thread\n");
284 return PTR_ERR(channel->playback_task);
288 if (most_start_channel(channel->iface, channel->id, &audio_aim)) {
289 pr_err("most_start_channel() failed!\n");
290 if (cfg->direction == MOST_CH_TX)
291 kthread_stop(channel->playback_task);
295 runtime->hw = channel->pcm_hardware;
300 * pcm_close - implements close callback function for PCM middle layer
301 * @substream: sub-stream pointer
303 * Obviously, this is called when a PCM substream is closed. Any private
304 * instance for a PCM substream allocated in the open callback will be
307 * Returns 0 on success or error code otherwise.
309 static int pcm_close(struct snd_pcm_substream *substream)
311 struct channel *channel = substream->private_data;
313 if (channel->cfg->direction == MOST_CH_TX)
314 kthread_stop(channel->playback_task);
315 most_stop_channel(channel->iface, channel->id, &audio_aim);
321 * pcm_hw_params - implements hw_params callback function for PCM middle layer
322 * @substream: sub-stream pointer
323 * @hw_params: contains the hardware parameters set by the application
325 * This is called when the hardware parameters is set by the application, that
326 * is, once when the buffer size, the period size, the format, etc. are defined
327 * for the PCM substream. Many hardware setups should be done is this callback,
328 * including the allocation of buffers.
330 * Returns 0 on success or error code otherwise.
332 static int pcm_hw_params(struct snd_pcm_substream *substream,
333 struct snd_pcm_hw_params *hw_params)
335 struct channel *channel = substream->private_data;
337 if ((params_channels(hw_params) > channel->pcm_hardware.channels_max) ||
338 (params_channels(hw_params) < channel->pcm_hardware.channels_min)) {
339 pr_err("Requested number of channels not supported.\n");
342 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
343 params_buffer_bytes(hw_params));
347 * pcm_hw_free - implements hw_free callback function for PCM middle layer
348 * @substream: substream pointer
350 * This is called to release the resources allocated via hw_params.
351 * This function will be always called before the close callback is called.
353 * Returns 0 on success or error code otherwise.
355 static int pcm_hw_free(struct snd_pcm_substream *substream)
357 return snd_pcm_lib_free_vmalloc_buffer(substream);
361 * pcm_prepare - implements prepare callback function for PCM middle layer
362 * @substream: substream pointer
364 * This callback is called when the PCM is "prepared". Format rate, sample rate,
365 * etc., can be set here. This callback can be called many times at each setup.
367 * Returns 0 on success or error code otherwise.
369 static int pcm_prepare(struct snd_pcm_substream *substream)
371 struct channel *channel = substream->private_data;
372 struct snd_pcm_runtime *runtime = substream->runtime;
373 struct most_channel_config *cfg = channel->cfg;
374 int width = snd_pcm_format_physical_width(runtime->format);
376 channel->copy_fn = NULL;
378 if (cfg->direction == MOST_CH_TX) {
379 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
380 channel->copy_fn = alsa_to_most_memcpy;
381 else if (width == 16)
382 channel->copy_fn = alsa_to_most_copy16;
383 else if (width == 24)
384 channel->copy_fn = alsa_to_most_copy24;
385 else if (width == 32)
386 channel->copy_fn = alsa_to_most_copy32;
388 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
389 channel->copy_fn = most_to_alsa_memcpy;
390 else if (width == 16)
391 channel->copy_fn = most_to_alsa_copy16;
392 else if (width == 24)
393 channel->copy_fn = most_to_alsa_copy24;
394 else if (width == 32)
395 channel->copy_fn = most_to_alsa_copy32;
398 if (!channel->copy_fn) {
399 pr_err("unsupported format\n");
403 channel->period_pos = 0;
404 channel->buffer_pos = 0;
410 * pcm_trigger - implements trigger callback function for PCM middle layer
411 * @substream: substream pointer
412 * @cmd: action to perform
414 * This is called when the PCM is started, stopped or paused. The action will be
415 * specified in the second argument, SNDRV_PCM_TRIGGER_XXX
417 * Returns 0 on success or error code otherwise.
419 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
421 struct channel *channel = substream->private_data;
424 case SNDRV_PCM_TRIGGER_START:
425 channel->is_stream_running = true;
426 wake_up_interruptible(&channel->playback_waitq);
429 case SNDRV_PCM_TRIGGER_STOP:
430 channel->is_stream_running = false;
434 pr_info("pcm_trigger(), invalid\n");
441 * pcm_pointer - implements pointer callback function for PCM middle layer
442 * @substream: substream pointer
444 * This callback is called when the PCM middle layer inquires the current
445 * hardware position on the buffer. The position must be returned in frames,
446 * ranging from 0 to buffer_size-1.
448 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
450 struct channel *channel = substream->private_data;
452 return channel->buffer_pos;
456 * Initialization of struct snd_pcm_ops
458 static const struct snd_pcm_ops pcm_ops = {
461 .ioctl = snd_pcm_lib_ioctl,
462 .hw_params = pcm_hw_params,
463 .hw_free = pcm_hw_free,
464 .prepare = pcm_prepare,
465 .trigger = pcm_trigger,
466 .pointer = pcm_pointer,
467 .page = snd_pcm_lib_get_vmalloc_page,
468 .mmap = snd_pcm_lib_mmap_vmalloc,
471 static int split_arg_list(char *buf, char **card_name, char **pcm_format)
473 *card_name = strsep(&buf, ".");
476 *pcm_format = strsep(&buf, ".\n");
482 static int audio_set_hw_params(struct snd_pcm_hardware *pcm_hw,
484 struct most_channel_config *cfg)
486 pcm_hw->info = MOST_PCM_INFO;
487 pcm_hw->rates = SNDRV_PCM_RATE_48000;
488 pcm_hw->rate_min = 48000;
489 pcm_hw->rate_max = 48000;
490 pcm_hw->buffer_bytes_max = cfg->num_buffers * cfg->buffer_size;
491 pcm_hw->period_bytes_min = cfg->buffer_size;
492 pcm_hw->period_bytes_max = cfg->buffer_size;
493 pcm_hw->periods_min = 1;
494 pcm_hw->periods_max = cfg->num_buffers;
496 if (!strcmp(pcm_format, "1x8")) {
497 if (cfg->subbuffer_size != 1)
499 pr_info("PCM format is 8-bit mono\n");
500 pcm_hw->channels_min = 1;
501 pcm_hw->channels_max = 1;
502 pcm_hw->formats = SNDRV_PCM_FMTBIT_S8;
503 } else if (!strcmp(pcm_format, "2x16")) {
504 if (cfg->subbuffer_size != 4)
506 pr_info("PCM format is 16-bit stereo\n");
507 pcm_hw->channels_min = 2;
508 pcm_hw->channels_max = 2;
509 pcm_hw->formats = SNDRV_PCM_FMTBIT_S16_LE |
510 SNDRV_PCM_FMTBIT_S16_BE;
511 } else if (!strcmp(pcm_format, "2x24")) {
512 if (cfg->subbuffer_size != 6)
514 pr_info("PCM format is 24-bit stereo\n");
515 pcm_hw->channels_min = 2;
516 pcm_hw->channels_max = 2;
517 pcm_hw->formats = SNDRV_PCM_FMTBIT_S24_3LE |
518 SNDRV_PCM_FMTBIT_S24_3BE;
519 } else if (!strcmp(pcm_format, "2x32")) {
520 if (cfg->subbuffer_size != 8)
522 pr_info("PCM format is 32-bit stereo\n");
523 pcm_hw->channels_min = 2;
524 pcm_hw->channels_max = 2;
525 pcm_hw->formats = SNDRV_PCM_FMTBIT_S32_LE |
526 SNDRV_PCM_FMTBIT_S32_BE;
527 } else if (!strcmp(pcm_format, "6x16")) {
528 if (cfg->subbuffer_size != 12)
530 pr_info("PCM format is 16-bit 5.1 multi channel\n");
531 pcm_hw->channels_min = 6;
532 pcm_hw->channels_max = 6;
533 pcm_hw->formats = SNDRV_PCM_FMTBIT_S16_LE |
534 SNDRV_PCM_FMTBIT_S16_BE;
536 pr_err("PCM format %s not supported\n", pcm_format);
541 pr_err("Audio resolution doesn't fit subbuffer size\n");
546 * audio_probe_channel - probe function of the driver module
547 * @iface: pointer to interface instance
548 * @channel_id: channel index/ID
549 * @cfg: pointer to actual channel configuration
550 * @parent: pointer to kobject (needed for sysfs hook-up)
551 * @arg_list: string that provides the name of the device to be created in /dev
552 * plus the desired audio resolution
554 * Creates sound card, pcm device, sets pcm ops and registers sound card.
556 * Returns 0 on success or error code otherwise.
558 static int audio_probe_channel(struct most_interface *iface, int channel_id,
559 struct most_channel_config *cfg,
560 struct kobject *parent, char *arg_list)
562 struct channel *channel;
563 struct snd_card *card;
565 int playback_count = 0;
566 int capture_count = 0;
575 if (cfg->data_type != MOST_CH_SYNC) {
576 pr_err("Incompatible channel type\n");
580 if (get_channel(iface, channel_id)) {
581 pr_err("channel (%s:%d) is already linked\n",
582 iface->description, channel_id);
586 if (cfg->direction == MOST_CH_TX) {
588 direction = SNDRV_PCM_STREAM_PLAYBACK;
591 direction = SNDRV_PCM_STREAM_CAPTURE;
594 ret = split_arg_list(arg_list, &card_name, &pcm_format);
596 pr_info("PCM format missing\n");
600 ret = snd_card_new(NULL, -1, card_name, THIS_MODULE,
601 sizeof(*channel), &card);
605 channel = card->private_data;
606 channel->card = card;
608 channel->iface = iface;
609 channel->id = channel_id;
610 init_waitqueue_head(&channel->playback_waitq);
612 ret = audio_set_hw_params(&channel->pcm_hardware, pcm_format, cfg);
616 snprintf(card->driver, sizeof(card->driver), "%s", DRIVER_NAME);
617 snprintf(card->shortname, sizeof(card->shortname), "Microchip MOST:%d",
619 snprintf(card->longname, sizeof(card->longname), "%s at %s, ch %d",
620 card->shortname, iface->description, channel_id);
622 ret = snd_pcm_new(card, card_name, 0, playback_count,
623 capture_count, &pcm);
627 pcm->private_data = channel;
629 snd_pcm_set_ops(pcm, direction, &pcm_ops);
631 ret = snd_card_register(card);
635 list_add_tail(&channel->list, &dev_list);
645 * audio_disconnect_channel - function to disconnect a channel
646 * @iface: pointer to interface instance
647 * @channel_id: channel index
649 * This frees allocated memory and removes the sound card from ALSA
651 * Returns 0 on success or error code otherwise.
653 static int audio_disconnect_channel(struct most_interface *iface,
656 struct channel *channel;
658 channel = get_channel(iface, channel_id);
660 pr_err("sound_disconnect_channel(), invalid channel %d\n",
665 list_del(&channel->list);
666 snd_card_free(channel->card);
672 * audio_rx_completion - completion handler for rx channels
673 * @mbo: pointer to buffer object that has completed
675 * This searches for the channel this MBO belongs to and copy the data from MBO
678 * Returns 0 on success or error code otherwise.
680 static int audio_rx_completion(struct mbo *mbo)
682 struct channel *channel = get_channel(mbo->ifp, mbo->hdm_channel_id);
683 bool period_elapsed = false;
686 pr_err("sound_rx_completion(), invalid channel %d\n",
687 mbo->hdm_channel_id);
691 if (channel->is_stream_running)
692 period_elapsed = copy_data(channel, mbo);
697 snd_pcm_period_elapsed(channel->substream);
703 * audio_tx_completion - completion handler for tx channels
704 * @iface: pointer to interface instance
705 * @channel_id: channel index/ID
707 * This searches the channel that belongs to this combination of interface
708 * pointer and channel ID and wakes a process sitting in the wait queue of
711 * Returns 0 on success or error code otherwise.
713 static int audio_tx_completion(struct most_interface *iface, int channel_id)
715 struct channel *channel = get_channel(iface, channel_id);
718 pr_err("sound_tx_completion(), invalid channel %d\n",
723 wake_up_interruptible(&channel->playback_waitq);
729 * Initialization of the struct most_aim
731 static struct most_aim audio_aim = {
733 .probe_channel = audio_probe_channel,
734 .disconnect_channel = audio_disconnect_channel,
735 .rx_completion = audio_rx_completion,
736 .tx_completion = audio_tx_completion,
739 static int __init audio_init(void)
743 INIT_LIST_HEAD(&dev_list);
745 return most_register_aim(&audio_aim);
748 static void __exit audio_exit(void)
750 struct channel *channel, *tmp;
754 list_for_each_entry_safe(channel, tmp, &dev_list, list) {
755 list_del(&channel->list);
756 snd_card_free(channel->card);
759 most_deregister_aim(&audio_aim);
762 module_init(audio_init);
763 module_exit(audio_exit);
765 MODULE_LICENSE("GPL");
766 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
767 MODULE_DESCRIPTION("Audio Application Interface Module for MostCore");