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 <linux/sched.h>
23 #include <linux/kthread.h>
26 #define DRIVER_NAME "sound"
28 static struct list_head dev_list;
31 * struct channel - private structure to keep channel specific data
32 * @substream: stores the substream structure
33 * @iface: interface for which the channel belongs to
34 * @cfg: channel configuration
35 * @card: registered sound card
36 * @list: list for private use
38 * @period_pos: current period position (ring buffer)
39 * @buffer_pos: current buffer position (ring buffer)
40 * @is_stream_running: identifies whether a stream is running or not
41 * @opened: set when the stream is opened
42 * @playback_task: playback thread
43 * @playback_waitq: waitq used by playback thread
46 struct snd_pcm_substream *substream;
47 struct most_interface *iface;
48 struct most_channel_config *cfg;
49 struct snd_card *card;
50 struct list_head list;
52 unsigned int period_pos;
53 unsigned int buffer_pos;
54 bool is_stream_running;
56 struct task_struct *playback_task;
57 wait_queue_head_t playback_waitq;
59 void (*copy_fn)(void *alsa, void *most, unsigned int bytes);
62 #define MOST_PCM_INFO (SNDRV_PCM_INFO_MMAP | \
63 SNDRV_PCM_INFO_MMAP_VALID | \
64 SNDRV_PCM_INFO_BATCH | \
65 SNDRV_PCM_INFO_INTERLEAVED | \
66 SNDRV_PCM_INFO_BLOCK_TRANSFER)
69 * Initialization of struct snd_pcm_hardware
71 static struct snd_pcm_hardware pcm_hardware_template = {
72 .info = MOST_PCM_INFO,
73 .rates = SNDRV_PCM_RATE_48000,
80 #define swap16(val) ( \
81 (((u16)(val) << 8) & (u16)0xFF00) | \
82 (((u16)(val) >> 8) & (u16)0x00FF))
84 #define swap32(val) ( \
85 (((u32)(val) << 24) & (u32)0xFF000000) | \
86 (((u32)(val) << 8) & (u32)0x00FF0000) | \
87 (((u32)(val) >> 8) & (u32)0x0000FF00) | \
88 (((u32)(val) >> 24) & (u32)0x000000FF))
90 static void swap_copy16(u16 *dest, const u16 *source, unsigned int bytes)
94 while (i < (bytes / 2)) {
95 dest[i] = swap16(source[i]);
100 static void swap_copy24(u8 *dest, const u8 *source, unsigned int bytes)
104 while (i < bytes - 2) {
105 dest[i] = source[i + 2];
106 dest[i + 1] = source[i + 1];
107 dest[i + 2] = source[i];
112 static void swap_copy32(u32 *dest, const u32 *source, unsigned int bytes)
116 while (i < bytes / 4) {
117 dest[i] = swap32(source[i]);
122 static void alsa_to_most_memcpy(void *alsa, void *most, unsigned int bytes)
124 memcpy(most, alsa, bytes);
127 static void alsa_to_most_copy16(void *alsa, void *most, unsigned int bytes)
129 swap_copy16(most, alsa, bytes);
132 static void alsa_to_most_copy24(void *alsa, void *most, unsigned int bytes)
134 swap_copy24(most, alsa, bytes);
137 static void alsa_to_most_copy32(void *alsa, void *most, unsigned int bytes)
139 swap_copy32(most, alsa, bytes);
142 static void most_to_alsa_memcpy(void *alsa, void *most, unsigned int bytes)
144 memcpy(alsa, most, bytes);
147 static void most_to_alsa_copy16(void *alsa, void *most, unsigned int bytes)
149 swap_copy16(alsa, most, bytes);
152 static void most_to_alsa_copy24(void *alsa, void *most, unsigned int bytes)
154 swap_copy24(alsa, most, bytes);
157 static void most_to_alsa_copy32(void *alsa, void *most, unsigned int bytes)
159 swap_copy32(alsa, most, bytes);
163 * get_channel - get pointer to channel
164 * @iface: interface structure
165 * @channel_id: channel ID
167 * This traverses the channel list and returns the channel matching the
170 * Returns pointer to channel on success or NULL otherwise.
172 static struct channel *get_channel(struct most_interface *iface,
175 struct channel *channel, *tmp;
177 list_for_each_entry_safe(channel, tmp, &dev_list, list) {
178 if ((channel->iface == iface) && (channel->id == channel_id))
186 * copy_data - implements data copying function
188 * @mbo: MBO from core
190 * Copy data from/to ring buffer to/from MBO and update the buffer position
192 static bool copy_data(struct channel *channel, struct mbo *mbo)
194 struct snd_pcm_runtime *const runtime = channel->substream->runtime;
195 unsigned int const frame_bytes = channel->cfg->subbuffer_size;
196 unsigned int const buffer_size = runtime->buffer_size;
200 if (channel->cfg->direction & MOST_CH_RX)
201 frames = mbo->processed_length / frame_bytes;
203 frames = mbo->buffer_length / frame_bytes;
204 fr0 = min(buffer_size - channel->buffer_pos, frames);
206 channel->copy_fn(runtime->dma_area + channel->buffer_pos * frame_bytes,
211 /* wrap around at end of ring buffer */
212 channel->copy_fn(runtime->dma_area,
213 mbo->virt_address + fr0 * frame_bytes,
214 (frames - fr0) * frame_bytes);
217 channel->buffer_pos += frames;
218 if (channel->buffer_pos >= buffer_size)
219 channel->buffer_pos -= buffer_size;
220 channel->period_pos += frames;
221 if (channel->period_pos >= runtime->period_size) {
222 channel->period_pos -= runtime->period_size;
230 * playback_thread - function implements the playback thread
231 * @data: private data
233 * Thread which does the playback functionality in a loop. It waits for a free
234 * MBO from mostcore for a particular channel and copy the data from ring buffer
235 * to MBO. Submit the MBO back to mostcore, after copying the data.
237 * Returns 0 on success or error code otherwise.
239 static int playback_thread(void *data)
241 struct channel *const channel = data;
243 pr_info("playback thread started\n");
245 while (!kthread_should_stop()) {
246 struct mbo *mbo = NULL;
247 bool period_elapsed = false;
250 wait_event_interruptible(
251 channel->playback_waitq,
252 kthread_should_stop() ||
253 (mbo = most_get_mbo(channel->iface, channel->id)));
258 if (channel->is_stream_running)
259 period_elapsed = copy_data(channel, mbo);
261 memset(mbo->virt_address, 0, mbo->buffer_length);
263 ret = most_submit_mbo(mbo);
265 channel->is_stream_running = false;
268 snd_pcm_period_elapsed(channel->substream);
275 * pcm_open - implements open callback function for PCM middle layer
276 * @substream: pointer to ALSA PCM substream
278 * This is called when a PCM substream is opened. At least, the function should
279 * initialize the runtime->hw record.
281 * Returns 0 on success or error code otherwise.
283 static int pcm_open(struct snd_pcm_substream *substream)
285 struct channel *channel = substream->private_data;
286 struct snd_pcm_runtime *runtime = substream->runtime;
287 struct most_channel_config *cfg = channel->cfg;
289 pr_info("pcm_open(), %s\n", substream->name);
291 channel->substream = substream;
293 if (cfg->direction == MOST_CH_TX) {
294 init_waitqueue_head(&channel->playback_waitq);
295 channel->playback_task = kthread_run(&playback_thread, channel,
296 "most_audio_playback");
297 if (IS_ERR(channel->playback_task))
298 return PTR_ERR(channel->playback_task);
301 if (most_start_channel(channel->iface, channel->id)) {
302 pr_err("most_start_channel() failed!\n");
303 if (cfg->direction == MOST_CH_TX)
304 kthread_stop(channel->playback_task);
308 runtime->hw = pcm_hardware_template;
309 runtime->hw.buffer_bytes_max = cfg->num_buffers * cfg->buffer_size;
310 runtime->hw.period_bytes_min = cfg->buffer_size;
311 runtime->hw.period_bytes_max = cfg->buffer_size;
312 runtime->hw.periods_min = 1;
313 runtime->hw.periods_max = cfg->num_buffers;
319 * pcm_close - implements close callback function for PCM middle layer
320 * @substream: sub-stream pointer
322 * Obviously, this is called when a PCM substream is closed. Any private
323 * instance for a PCM substream allocated in the open callback will be
326 * Returns 0 on success or error code otherwise.
328 static int pcm_close(struct snd_pcm_substream *substream)
330 struct channel *channel = substream->private_data;
332 pr_info("pcm_close(), %s\n", substream->name);
334 if (channel->cfg->direction == MOST_CH_TX)
335 kthread_stop(channel->playback_task);
336 most_stop_channel(channel->iface, channel->id);
342 * pcm_hw_params - implements hw_params callback function for PCM middle layer
343 * @substream: sub-stream pointer
344 * @hw_params: contains the hardware parameters set by the application
346 * This is called when the hardware parameters is set by the application, that
347 * is, once when the buffer size, the period size, the format, etc. are defined
348 * for the PCM substream. Many hardware setups should be done is this callback,
349 * including the allocation of buffers.
351 * Returns 0 on success or error code otherwise.
353 static int pcm_hw_params(struct snd_pcm_substream *substream,
354 struct snd_pcm_hw_params *hw_params)
356 pr_info("pcm_hw_params()\n");
358 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
359 params_buffer_bytes(hw_params));
363 * pcm_hw_free - implements hw_free callback function for PCM middle layer
364 * @substream: substream pointer
366 * This is called to release the resources allocated via hw_params.
367 * This function will be always called before the close callback is called.
369 * Returns 0 on success or error code otherwise.
371 static int pcm_hw_free(struct snd_pcm_substream *substream)
373 pr_info("pcm_hw_free()\n");
375 return snd_pcm_lib_free_vmalloc_buffer(substream);
379 * pcm_prepare - implements prepare callback function for PCM middle layer
380 * @substream: substream pointer
382 * This callback is called when the PCM is "prepared". Format rate, sample rate,
383 * etc., can be set here. This callback can be called many times at each setup.
385 * Returns 0 on success or error code otherwise.
387 static int pcm_prepare(struct snd_pcm_substream *substream)
389 struct channel *channel = substream->private_data;
390 struct snd_pcm_runtime *runtime = substream->runtime;
391 struct most_channel_config *cfg = channel->cfg;
392 int width = snd_pcm_format_physical_width(runtime->format);
394 channel->copy_fn = NULL;
396 if (cfg->direction == MOST_CH_TX) {
397 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
398 channel->copy_fn = alsa_to_most_memcpy;
399 else if (width == 16)
400 channel->copy_fn = alsa_to_most_copy16;
401 else if (width == 24)
402 channel->copy_fn = alsa_to_most_copy24;
403 else if (width == 32)
404 channel->copy_fn = alsa_to_most_copy32;
406 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
407 channel->copy_fn = most_to_alsa_memcpy;
408 else if (width == 16)
409 channel->copy_fn = most_to_alsa_copy16;
410 else if (width == 24)
411 channel->copy_fn = most_to_alsa_copy24;
412 else if (width == 32)
413 channel->copy_fn = most_to_alsa_copy32;
416 if (!channel->copy_fn) {
417 pr_err("unsupported format\n");
421 channel->period_pos = 0;
422 channel->buffer_pos = 0;
428 * pcm_trigger - implements trigger callback function for PCM middle layer
429 * @substream: substream pointer
430 * @cmd: action to perform
432 * This is called when the PCM is started, stopped or paused. The action will be
433 * specified in the second argument, SNDRV_PCM_TRIGGER_XXX
435 * Returns 0 on success or error code otherwise.
437 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
439 struct channel *channel = substream->private_data;
442 case SNDRV_PCM_TRIGGER_START:
443 channel->is_stream_running = true;
446 case SNDRV_PCM_TRIGGER_STOP:
447 channel->is_stream_running = false;
451 pr_info("pcm_trigger(), invalid\n");
458 * pcm_pointer - implements pointer callback function for PCM middle layer
459 * @substream: substream pointer
461 * This callback is called when the PCM middle layer inquires the current
462 * hardware position on the buffer. The position must be returned in frames,
463 * ranging from 0 to buffer_size-1.
465 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
467 struct channel *channel = substream->private_data;
469 return channel->buffer_pos;
473 * Initialization of struct snd_pcm_ops
475 static struct snd_pcm_ops pcm_ops = {
478 .ioctl = snd_pcm_lib_ioctl,
479 .hw_params = pcm_hw_params,
480 .hw_free = pcm_hw_free,
481 .prepare = pcm_prepare,
482 .trigger = pcm_trigger,
483 .pointer = pcm_pointer,
484 .page = snd_pcm_lib_get_vmalloc_page,
485 .mmap = snd_pcm_lib_mmap_vmalloc,
489 static int split_arg_list(char *buf, char **card_name, char **pcm_format)
491 *card_name = strsep(&buf, ".");
494 *pcm_format = strsep(&buf, ".\n");
500 static int audio_set_pcm_format(char *pcm_format,
501 struct most_channel_config *cfg)
503 if (!strcmp(pcm_format, "1x8")) {
504 if (cfg->subbuffer_size != 1)
506 pr_info("PCM format is 8-bit mono\n");
507 pcm_hardware_template.formats = SNDRV_PCM_FMTBIT_S8;
508 } else if (!strcmp(pcm_format, "2x16")) {
509 if (cfg->subbuffer_size != 4)
511 pr_info("PCM format is 16-bit stereo\n");
512 pcm_hardware_template.formats = SNDRV_PCM_FMTBIT_S16_LE |
513 SNDRV_PCM_FMTBIT_S16_BE;
514 } else if (!strcmp(pcm_format, "2x24")) {
515 if (cfg->subbuffer_size != 6)
517 pr_info("PCM format is 24-bit stereo\n");
518 pcm_hardware_template.formats = SNDRV_PCM_FMTBIT_S24_3LE |
519 SNDRV_PCM_FMTBIT_S24_3BE;
520 } else if (!strcmp(pcm_format, "2x32")) {
521 if (cfg->subbuffer_size != 8)
523 pr_info("PCM format is 32-bit stereo\n");
524 pcm_hardware_template.formats = SNDRV_PCM_FMTBIT_S32_LE |
525 SNDRV_PCM_FMTBIT_S32_BE;
527 pr_err("PCM format %s not supported\n", pcm_format);
532 pr_err("Audio resolution doesn't fit subbuffer size\n");
537 * audio_probe_channel - probe function of the driver module
538 * @iface: pointer to interface instance
539 * @channel_id: channel index/ID
540 * @cfg: pointer to actual channel configuration
541 * @parent: pointer to kobject (needed for sysfs hook-up)
542 * @arg_list: string that provides the name of the device to be created in /dev
543 * plus the desired audio resolution
545 * Creates sound card, pcm device, sets pcm ops and registers sound card.
547 * Returns 0 on success or error code otherwise.
549 static int audio_probe_channel(struct most_interface *iface, int channel_id,
550 struct most_channel_config *cfg,
551 struct kobject *parent, char *arg_list)
553 struct channel *channel;
554 struct snd_card *card;
556 int playback_count = 0;
557 int capture_count = 0;
563 pr_info("sound_probe_channel()\n");
568 if (cfg->data_type != MOST_CH_SYNC) {
569 pr_err("Incompatible channel type\n");
573 if (get_channel(iface, channel_id)) {
574 pr_err("channel (%s:%d) is already linked\n",
575 iface->description, channel_id);
579 if (cfg->direction == MOST_CH_TX) {
581 direction = SNDRV_PCM_STREAM_PLAYBACK;
584 direction = SNDRV_PCM_STREAM_CAPTURE;
587 ret = split_arg_list(arg_list, &card_name, &pcm_format);
589 pr_info("PCM format missing\n");
592 if (audio_set_pcm_format(pcm_format, cfg))
595 ret = snd_card_new(NULL, -1, card_name, THIS_MODULE,
596 sizeof(*channel), &card);
600 channel = card->private_data;
601 channel->card = card;
603 channel->iface = iface;
604 channel->id = channel_id;
606 snprintf(card->driver, sizeof(card->driver), "%s", DRIVER_NAME);
607 snprintf(card->shortname, sizeof(card->shortname), "MOST:%d",
609 snprintf(card->longname, sizeof(card->longname), "%s at %s, ch %d",
610 card->shortname, iface->description, channel_id);
612 ret = snd_pcm_new(card, card_name, 0, playback_count,
613 capture_count, &pcm);
617 pcm->private_data = channel;
619 snd_pcm_set_ops(pcm, direction, &pcm_ops);
621 ret = snd_card_register(card);
625 list_add_tail(&channel->list, &dev_list);
635 * audio_disconnect_channel - function to disconnect a channel
636 * @iface: pointer to interface instance
637 * @channel_id: channel index
639 * This frees allocated memory and removes the sound card from ALSA
641 * Returns 0 on success or error code otherwise.
643 static int audio_disconnect_channel(struct most_interface *iface,
646 struct channel *channel;
648 pr_info("sound_disconnect_channel()\n");
650 channel = get_channel(iface, channel_id);
652 pr_err("sound_disconnect_channel(), invalid channel %d\n",
657 list_del(&channel->list);
658 snd_card_free(channel->card);
664 * audio_rx_completion - completion handler for rx channels
665 * @mbo: pointer to buffer object that has completed
667 * This searches for the channel this MBO belongs to and copy the data from MBO
670 * Returns 0 on success or error code otherwise.
672 static int audio_rx_completion(struct mbo *mbo)
674 struct channel *channel = get_channel(mbo->ifp, mbo->hdm_channel_id);
675 bool period_elapsed = false;
678 pr_err("sound_rx_completion(), invalid channel %d\n",
679 mbo->hdm_channel_id);
683 if (channel->is_stream_running)
684 period_elapsed = copy_data(channel, mbo);
689 snd_pcm_period_elapsed(channel->substream);
695 * audio_tx_completion - completion handler for tx channels
696 * @iface: pointer to interface instance
697 * @channel_id: channel index/ID
699 * This searches the channel that belongs to this combination of interface
700 * pointer and channel ID and wakes a process sitting in the wait queue of
703 * Returns 0 on success or error code otherwise.
705 static int audio_tx_completion(struct most_interface *iface, int channel_id)
707 struct channel *channel = get_channel(iface, channel_id);
710 pr_err("sound_tx_completion(), invalid channel %d\n",
715 wake_up_interruptible(&channel->playback_waitq);
721 * Initialization of the struct most_aim
723 static struct most_aim audio_aim = {
725 .probe_channel = audio_probe_channel,
726 .disconnect_channel = audio_disconnect_channel,
727 .rx_completion = audio_rx_completion,
728 .tx_completion = audio_tx_completion,
731 static int __init audio_init(void)
735 INIT_LIST_HEAD(&dev_list);
737 return most_register_aim(&audio_aim);
740 static void __exit audio_exit(void)
742 struct channel *channel, *tmp;
746 list_for_each_entry_safe(channel, tmp, &dev_list, list) {
747 list_del(&channel->list);
748 snd_card_free(channel->card);
751 most_deregister_aim(&audio_aim);
754 module_init(audio_init);
755 module_exit(audio_exit);
757 MODULE_LICENSE("GPL");
758 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
759 MODULE_DESCRIPTION("Audio Application Interface Module for MostCore");