blob: e4198e5e064b5badaaa94136bc5ab274e0c64ad5 [file] [log] [blame]
Christian Gromm54b48562015-07-24 16:11:51 +02001/*
2 * sound.c - Audio Application Interface Module for Mostcore
3 *
4 * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
5 *
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.
10 *
11 * This file is licensed under GPLv2.
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
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>
Christian Gromm8e4a0ef2015-09-28 17:18:47 +020022#include <sound/pcm_params.h>
Christian Gromm54b48562015-07-24 16:11:51 +020023#include <linux/sched.h>
24#include <linux/kthread.h>
25#include <mostcore.h>
26
27#define DRIVER_NAME "sound"
28
29static struct list_head dev_list;
Christian Grommf13f6982015-09-28 17:18:35 +020030static struct most_aim audio_aim;
Christian Gromm54b48562015-07-24 16:11:51 +020031
32/**
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
39 * @id: channel index
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
46 */
47struct channel {
48 struct snd_pcm_substream *substream;
Christian Grommd8018872015-09-28 17:18:49 +020049 struct snd_pcm_hardware pcm_hardware;
Christian Gromm54b48562015-07-24 16:11:51 +020050 struct most_interface *iface;
51 struct most_channel_config *cfg;
52 struct snd_card *card;
53 struct list_head list;
54 int id;
55 unsigned int period_pos;
56 unsigned int buffer_pos;
57 bool is_stream_running;
58
59 struct task_struct *playback_task;
60 wait_queue_head_t playback_waitq;
61
62 void (*copy_fn)(void *alsa, void *most, unsigned int bytes);
63};
64
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)
70
Christian Gromm54b48562015-07-24 16:11:51 +020071#define swap16(val) ( \
72 (((u16)(val) << 8) & (u16)0xFF00) | \
73 (((u16)(val) >> 8) & (u16)0x00FF))
74
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))
80
81static void swap_copy16(u16 *dest, const u16 *source, unsigned int bytes)
82{
83 unsigned int i = 0;
84
85 while (i < (bytes / 2)) {
86 dest[i] = swap16(source[i]);
87 i++;
88 }
89}
90
91static void swap_copy24(u8 *dest, const u8 *source, unsigned int bytes)
92{
93 unsigned int i = 0;
94
95 while (i < bytes - 2) {
96 dest[i] = source[i + 2];
97 dest[i + 1] = source[i + 1];
98 dest[i + 2] = source[i];
99 i += 3;
100 }
101}
102
103static void swap_copy32(u32 *dest, const u32 *source, unsigned int bytes)
104{
105 unsigned int i = 0;
106
107 while (i < bytes / 4) {
108 dest[i] = swap32(source[i]);
109 i++;
110 }
111}
112
113static void alsa_to_most_memcpy(void *alsa, void *most, unsigned int bytes)
114{
115 memcpy(most, alsa, bytes);
116}
117
118static void alsa_to_most_copy16(void *alsa, void *most, unsigned int bytes)
119{
120 swap_copy16(most, alsa, bytes);
121}
122
123static void alsa_to_most_copy24(void *alsa, void *most, unsigned int bytes)
124{
125 swap_copy24(most, alsa, bytes);
126}
127
128static void alsa_to_most_copy32(void *alsa, void *most, unsigned int bytes)
129{
130 swap_copy32(most, alsa, bytes);
131}
132
133static void most_to_alsa_memcpy(void *alsa, void *most, unsigned int bytes)
134{
135 memcpy(alsa, most, bytes);
136}
137
138static void most_to_alsa_copy16(void *alsa, void *most, unsigned int bytes)
139{
140 swap_copy16(alsa, most, bytes);
141}
142
143static void most_to_alsa_copy24(void *alsa, void *most, unsigned int bytes)
144{
145 swap_copy24(alsa, most, bytes);
146}
147
148static void most_to_alsa_copy32(void *alsa, void *most, unsigned int bytes)
149{
150 swap_copy32(alsa, most, bytes);
151}
152
153/**
154 * get_channel - get pointer to channel
155 * @iface: interface structure
156 * @channel_id: channel ID
157 *
158 * This traverses the channel list and returns the channel matching the
159 * ID and interface.
160 *
161 * Returns pointer to channel on success or NULL otherwise.
162 */
163static struct channel *get_channel(struct most_interface *iface,
164 int channel_id)
165{
166 struct channel *channel, *tmp;
167
168 list_for_each_entry_safe(channel, tmp, &dev_list, list) {
169 if ((channel->iface == iface) && (channel->id == channel_id))
170 return channel;
171 }
172
173 return NULL;
174}
175
176/**
177 * copy_data - implements data copying function
178 * @channel: channel
179 * @mbo: MBO from core
180 *
181 * Copy data from/to ring buffer to/from MBO and update the buffer position
182 */
183static bool copy_data(struct channel *channel, struct mbo *mbo)
184{
185 struct snd_pcm_runtime *const runtime = channel->substream->runtime;
186 unsigned int const frame_bytes = channel->cfg->subbuffer_size;
187 unsigned int const buffer_size = runtime->buffer_size;
188 unsigned int frames;
189 unsigned int fr0;
190
191 if (channel->cfg->direction & MOST_CH_RX)
192 frames = mbo->processed_length / frame_bytes;
193 else
194 frames = mbo->buffer_length / frame_bytes;
195 fr0 = min(buffer_size - channel->buffer_pos, frames);
196
197 channel->copy_fn(runtime->dma_area + channel->buffer_pos * frame_bytes,
198 mbo->virt_address,
199 fr0 * frame_bytes);
200
201 if (frames > fr0) {
202 /* wrap around at end of ring buffer */
203 channel->copy_fn(runtime->dma_area,
204 mbo->virt_address + fr0 * frame_bytes,
205 (frames - fr0) * frame_bytes);
206 }
207
208 channel->buffer_pos += frames;
209 if (channel->buffer_pos >= buffer_size)
210 channel->buffer_pos -= buffer_size;
211 channel->period_pos += frames;
212 if (channel->period_pos >= runtime->period_size) {
213 channel->period_pos -= runtime->period_size;
214 return true;
215 }
216
217 return false;
218}
219
220/**
221 * playback_thread - function implements the playback thread
222 * @data: private data
223 *
224 * Thread which does the playback functionality in a loop. It waits for a free
225 * MBO from mostcore for a particular channel and copy the data from ring buffer
226 * to MBO. Submit the MBO back to mostcore, after copying the data.
227 *
228 * Returns 0 on success or error code otherwise.
229 */
230static int playback_thread(void *data)
231{
232 struct channel *const channel = data;
233
Christian Gromm54b48562015-07-24 16:11:51 +0200234 while (!kthread_should_stop()) {
235 struct mbo *mbo = NULL;
236 bool period_elapsed = false;
Christian Gromm54b48562015-07-24 16:11:51 +0200237
238 wait_event_interruptible(
239 channel->playback_waitq,
240 kthread_should_stop() ||
Christian Gromm5e8aa942015-09-28 17:18:58 +0200241 (channel->is_stream_running &&
242 (mbo = most_get_mbo(channel->iface, channel->id,
243 &audio_aim))));
Christian Gromm54b48562015-07-24 16:11:51 +0200244 if (!mbo)
245 continue;
246
247 if (channel->is_stream_running)
248 period_elapsed = copy_data(channel, mbo);
249 else
250 memset(mbo->virt_address, 0, mbo->buffer_length);
251
Christian Gromma6f9d842016-09-21 14:49:05 +0200252 most_submit_mbo(mbo);
Christian Gromm54b48562015-07-24 16:11:51 +0200253 if (period_elapsed)
254 snd_pcm_period_elapsed(channel->substream);
255 }
256
257 return 0;
258}
259
260/**
261 * pcm_open - implements open callback function for PCM middle layer
262 * @substream: pointer to ALSA PCM substream
263 *
264 * This is called when a PCM substream is opened. At least, the function should
265 * initialize the runtime->hw record.
266 *
267 * Returns 0 on success or error code otherwise.
268 */
269static int pcm_open(struct snd_pcm_substream *substream)
270{
271 struct channel *channel = substream->private_data;
272 struct snd_pcm_runtime *runtime = substream->runtime;
273 struct most_channel_config *cfg = channel->cfg;
274
Christian Gromm54b48562015-07-24 16:11:51 +0200275 channel->substream = substream;
276
277 if (cfg->direction == MOST_CH_TX) {
Shraddha Barke246ed512015-10-13 21:07:49 +0530278 channel->playback_task = kthread_run(playback_thread, channel,
Christian Gromm54b48562015-07-24 16:11:51 +0200279 "most_audio_playback");
Christian Gromm5e8aa942015-09-28 17:18:58 +0200280 if (IS_ERR(channel->playback_task)) {
281 pr_err("Couldn't start thread\n");
Christian Gromm54b48562015-07-24 16:11:51 +0200282 return PTR_ERR(channel->playback_task);
Christian Gromm5e8aa942015-09-28 17:18:58 +0200283 }
Christian Gromm54b48562015-07-24 16:11:51 +0200284 }
285
Christian Grommf13f6982015-09-28 17:18:35 +0200286 if (most_start_channel(channel->iface, channel->id, &audio_aim)) {
Christian Gromm54b48562015-07-24 16:11:51 +0200287 pr_err("most_start_channel() failed!\n");
288 if (cfg->direction == MOST_CH_TX)
289 kthread_stop(channel->playback_task);
290 return -EBUSY;
291 }
292
Christian Grommd8018872015-09-28 17:18:49 +0200293 runtime->hw = channel->pcm_hardware;
Christian Gromm54b48562015-07-24 16:11:51 +0200294 return 0;
295}
296
297/**
298 * pcm_close - implements close callback function for PCM middle layer
299 * @substream: sub-stream pointer
300 *
301 * Obviously, this is called when a PCM substream is closed. Any private
302 * instance for a PCM substream allocated in the open callback will be
303 * released here.
304 *
305 * Returns 0 on success or error code otherwise.
306 */
307static int pcm_close(struct snd_pcm_substream *substream)
308{
309 struct channel *channel = substream->private_data;
310
Christian Gromm54b48562015-07-24 16:11:51 +0200311 if (channel->cfg->direction == MOST_CH_TX)
312 kthread_stop(channel->playback_task);
Christian Grommf13f6982015-09-28 17:18:35 +0200313 most_stop_channel(channel->iface, channel->id, &audio_aim);
Christian Gromm54b48562015-07-24 16:11:51 +0200314
315 return 0;
316}
317
318/**
319 * pcm_hw_params - implements hw_params callback function for PCM middle layer
320 * @substream: sub-stream pointer
321 * @hw_params: contains the hardware parameters set by the application
322 *
323 * This is called when the hardware parameters is set by the application, that
324 * is, once when the buffer size, the period size, the format, etc. are defined
325 * for the PCM substream. Many hardware setups should be done is this callback,
326 * including the allocation of buffers.
327 *
328 * Returns 0 on success or error code otherwise.
329 */
330static int pcm_hw_params(struct snd_pcm_substream *substream,
331 struct snd_pcm_hw_params *hw_params)
332{
Christian Grommd8018872015-09-28 17:18:49 +0200333 struct channel *channel = substream->private_data;
Christian Gromm8e4a0ef2015-09-28 17:18:47 +0200334
Christian Grommd8018872015-09-28 17:18:49 +0200335 if ((params_channels(hw_params) > channel->pcm_hardware.channels_max) ||
Christian Gromm5e8aa942015-09-28 17:18:58 +0200336 (params_channels(hw_params) < channel->pcm_hardware.channels_min)) {
337 pr_err("Requested number of channels not supported.\n");
Christian Gromm8e4a0ef2015-09-28 17:18:47 +0200338 return -EINVAL;
Christian Gromm5e8aa942015-09-28 17:18:58 +0200339 }
Christian Grommb981abb2015-09-28 17:18:51 +0200340 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
Christian Gromm35c33722015-09-28 17:18:55 +0200341 params_buffer_bytes(hw_params));
Christian Gromm54b48562015-07-24 16:11:51 +0200342}
343
344/**
345 * pcm_hw_free - implements hw_free callback function for PCM middle layer
346 * @substream: substream pointer
347 *
348 * This is called to release the resources allocated via hw_params.
349 * This function will be always called before the close callback is called.
350 *
351 * Returns 0 on success or error code otherwise.
352 */
353static int pcm_hw_free(struct snd_pcm_substream *substream)
354{
Christian Gromm54b48562015-07-24 16:11:51 +0200355 return snd_pcm_lib_free_vmalloc_buffer(substream);
356}
357
358/**
359 * pcm_prepare - implements prepare callback function for PCM middle layer
360 * @substream: substream pointer
361 *
362 * This callback is called when the PCM is "prepared". Format rate, sample rate,
363 * etc., can be set here. This callback can be called many times at each setup.
364 *
365 * Returns 0 on success or error code otherwise.
366 */
367static int pcm_prepare(struct snd_pcm_substream *substream)
368{
369 struct channel *channel = substream->private_data;
370 struct snd_pcm_runtime *runtime = substream->runtime;
371 struct most_channel_config *cfg = channel->cfg;
372 int width = snd_pcm_format_physical_width(runtime->format);
373
374 channel->copy_fn = NULL;
375
376 if (cfg->direction == MOST_CH_TX) {
377 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
378 channel->copy_fn = alsa_to_most_memcpy;
379 else if (width == 16)
380 channel->copy_fn = alsa_to_most_copy16;
381 else if (width == 24)
382 channel->copy_fn = alsa_to_most_copy24;
383 else if (width == 32)
384 channel->copy_fn = alsa_to_most_copy32;
385 } else {
386 if (snd_pcm_format_big_endian(runtime->format) || width == 8)
387 channel->copy_fn = most_to_alsa_memcpy;
388 else if (width == 16)
389 channel->copy_fn = most_to_alsa_copy16;
390 else if (width == 24)
391 channel->copy_fn = most_to_alsa_copy24;
392 else if (width == 32)
393 channel->copy_fn = most_to_alsa_copy32;
394 }
395
396 if (!channel->copy_fn) {
397 pr_err("unsupported format\n");
398 return -EINVAL;
399 }
400
401 channel->period_pos = 0;
402 channel->buffer_pos = 0;
403
404 return 0;
405}
406
407/**
408 * pcm_trigger - implements trigger callback function for PCM middle layer
409 * @substream: substream pointer
410 * @cmd: action to perform
411 *
412 * This is called when the PCM is started, stopped or paused. The action will be
413 * specified in the second argument, SNDRV_PCM_TRIGGER_XXX
414 *
415 * Returns 0 on success or error code otherwise.
416 */
417static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
418{
419 struct channel *channel = substream->private_data;
420
421 switch (cmd) {
422 case SNDRV_PCM_TRIGGER_START:
423 channel->is_stream_running = true;
Christian Gromm5e8aa942015-09-28 17:18:58 +0200424 wake_up_interruptible(&channel->playback_waitq);
Christian Gromm54b48562015-07-24 16:11:51 +0200425 return 0;
426
427 case SNDRV_PCM_TRIGGER_STOP:
428 channel->is_stream_running = false;
429 return 0;
430
431 default:
432 pr_info("pcm_trigger(), invalid\n");
433 return -EINVAL;
434 }
435 return 0;
436}
437
438/**
439 * pcm_pointer - implements pointer callback function for PCM middle layer
440 * @substream: substream pointer
441 *
442 * This callback is called when the PCM middle layer inquires the current
443 * hardware position on the buffer. The position must be returned in frames,
444 * ranging from 0 to buffer_size-1.
445 */
446static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
447{
448 struct channel *channel = substream->private_data;
449
450 return channel->buffer_pos;
451}
452
453/**
454 * Initialization of struct snd_pcm_ops
455 */
Bhumika Goyalcfb459e2016-09-26 23:09:03 +0530456static const struct snd_pcm_ops pcm_ops = {
Christian Gromm54b48562015-07-24 16:11:51 +0200457 .open = pcm_open,
458 .close = pcm_close,
459 .ioctl = snd_pcm_lib_ioctl,
460 .hw_params = pcm_hw_params,
461 .hw_free = pcm_hw_free,
462 .prepare = pcm_prepare,
463 .trigger = pcm_trigger,
464 .pointer = pcm_pointer,
465 .page = snd_pcm_lib_get_vmalloc_page,
466 .mmap = snd_pcm_lib_mmap_vmalloc,
467};
468
Sudip Mukherjee809da8a2015-08-18 20:48:25 +0530469static int split_arg_list(char *buf, char **card_name, char **pcm_format)
Christian Gromm54b48562015-07-24 16:11:51 +0200470{
471 *card_name = strsep(&buf, ".");
472 if (!*card_name)
473 return -EIO;
474 *pcm_format = strsep(&buf, ".\n");
475 if (!*pcm_format)
476 return -EIO;
477 return 0;
478}
479
Christian Grommfb0056a2015-09-28 17:18:53 +0200480static int audio_set_hw_params(struct snd_pcm_hardware *pcm_hw,
Christian Gromm35c33722015-09-28 17:18:55 +0200481 char *pcm_format,
482 struct most_channel_config *cfg)
Christian Gromm54b48562015-07-24 16:11:51 +0200483{
Christian Gromme569da22015-09-28 17:18:54 +0200484 pcm_hw->info = MOST_PCM_INFO;
485 pcm_hw->rates = SNDRV_PCM_RATE_48000;
486 pcm_hw->rate_min = 48000;
487 pcm_hw->rate_max = 48000;
488 pcm_hw->buffer_bytes_max = cfg->num_buffers * cfg->buffer_size;
489 pcm_hw->period_bytes_min = cfg->buffer_size;
490 pcm_hw->period_bytes_max = cfg->buffer_size;
491 pcm_hw->periods_min = 1;
492 pcm_hw->periods_max = cfg->num_buffers;
493
Christian Gromm54b48562015-07-24 16:11:51 +0200494 if (!strcmp(pcm_format, "1x8")) {
495 if (cfg->subbuffer_size != 1)
496 goto error;
497 pr_info("PCM format is 8-bit mono\n");
Christian Gromm31e91e02015-09-28 17:18:52 +0200498 pcm_hw->channels_min = 1;
499 pcm_hw->channels_max = 1;
Christian Grommd8018872015-09-28 17:18:49 +0200500 pcm_hw->formats = SNDRV_PCM_FMTBIT_S8;
Christian Gromm54b48562015-07-24 16:11:51 +0200501 } else if (!strcmp(pcm_format, "2x16")) {
502 if (cfg->subbuffer_size != 4)
503 goto error;
504 pr_info("PCM format is 16-bit stereo\n");
Christian Grommd8018872015-09-28 17:18:49 +0200505 pcm_hw->channels_min = 2;
506 pcm_hw->channels_max = 2;
507 pcm_hw->formats = SNDRV_PCM_FMTBIT_S16_LE |
508 SNDRV_PCM_FMTBIT_S16_BE;
Christian Gromm54b48562015-07-24 16:11:51 +0200509 } else if (!strcmp(pcm_format, "2x24")) {
510 if (cfg->subbuffer_size != 6)
511 goto error;
512 pr_info("PCM format is 24-bit stereo\n");
Christian Grommd8018872015-09-28 17:18:49 +0200513 pcm_hw->channels_min = 2;
514 pcm_hw->channels_max = 2;
515 pcm_hw->formats = SNDRV_PCM_FMTBIT_S24_3LE |
516 SNDRV_PCM_FMTBIT_S24_3BE;
Christian Gromm54b48562015-07-24 16:11:51 +0200517 } else if (!strcmp(pcm_format, "2x32")) {
518 if (cfg->subbuffer_size != 8)
519 goto error;
520 pr_info("PCM format is 32-bit stereo\n");
Christian Grommd8018872015-09-28 17:18:49 +0200521 pcm_hw->channels_min = 2;
522 pcm_hw->channels_max = 2;
523 pcm_hw->formats = SNDRV_PCM_FMTBIT_S32_LE |
524 SNDRV_PCM_FMTBIT_S32_BE;
Christian Gromm8e4a0ef2015-09-28 17:18:47 +0200525 } else if (!strcmp(pcm_format, "6x16")) {
526 if (cfg->subbuffer_size != 12)
527 goto error;
528 pr_info("PCM format is 16-bit 5.1 multi channel\n");
Christian Grommd8018872015-09-28 17:18:49 +0200529 pcm_hw->channels_min = 6;
530 pcm_hw->channels_max = 6;
531 pcm_hw->formats = SNDRV_PCM_FMTBIT_S16_LE |
532 SNDRV_PCM_FMTBIT_S16_BE;
Christian Gromm54b48562015-07-24 16:11:51 +0200533 } else {
534 pr_err("PCM format %s not supported\n", pcm_format);
535 return -EIO;
536 }
537 return 0;
538error:
539 pr_err("Audio resolution doesn't fit subbuffer size\n");
540 return -EINVAL;
541}
542
543/**
544 * audio_probe_channel - probe function of the driver module
545 * @iface: pointer to interface instance
546 * @channel_id: channel index/ID
547 * @cfg: pointer to actual channel configuration
548 * @parent: pointer to kobject (needed for sysfs hook-up)
549 * @arg_list: string that provides the name of the device to be created in /dev
550 * plus the desired audio resolution
551 *
552 * Creates sound card, pcm device, sets pcm ops and registers sound card.
553 *
554 * Returns 0 on success or error code otherwise.
555 */
556static int audio_probe_channel(struct most_interface *iface, int channel_id,
557 struct most_channel_config *cfg,
558 struct kobject *parent, char *arg_list)
559{
560 struct channel *channel;
561 struct snd_card *card;
562 struct snd_pcm *pcm;
563 int playback_count = 0;
564 int capture_count = 0;
565 int ret;
566 int direction;
567 char *card_name;
568 char *pcm_format;
569
Christian Gromm54b48562015-07-24 16:11:51 +0200570 if (!iface)
571 return -EINVAL;
572
573 if (cfg->data_type != MOST_CH_SYNC) {
574 pr_err("Incompatible channel type\n");
575 return -EINVAL;
576 }
577
578 if (get_channel(iface, channel_id)) {
579 pr_err("channel (%s:%d) is already linked\n",
580 iface->description, channel_id);
581 return -EINVAL;
582 }
583
584 if (cfg->direction == MOST_CH_TX) {
585 playback_count = 1;
586 direction = SNDRV_PCM_STREAM_PLAYBACK;
587 } else {
588 capture_count = 1;
589 direction = SNDRV_PCM_STREAM_CAPTURE;
590 }
591
592 ret = split_arg_list(arg_list, &card_name, &pcm_format);
593 if (ret < 0) {
594 pr_info("PCM format missing\n");
595 return ret;
596 }
Christian Gromm54b48562015-07-24 16:11:51 +0200597
598 ret = snd_card_new(NULL, -1, card_name, THIS_MODULE,
599 sizeof(*channel), &card);
600 if (ret < 0)
601 return ret;
602
603 channel = card->private_data;
604 channel->card = card;
605 channel->cfg = cfg;
606 channel->iface = iface;
607 channel->id = channel_id;
Christian Gromm5e8aa942015-09-28 17:18:58 +0200608 init_waitqueue_head(&channel->playback_waitq);
Christian Grommd8018872015-09-28 17:18:49 +0200609
Wei Yongjunca8b3fa2016-09-25 15:41:11 +0000610 ret = audio_set_hw_params(&channel->pcm_hardware, pcm_format, cfg);
611 if (ret)
Christian Grommd8018872015-09-28 17:18:49 +0200612 goto err_free_card;
Christian Gromm54b48562015-07-24 16:11:51 +0200613
614 snprintf(card->driver, sizeof(card->driver), "%s", DRIVER_NAME);
Christian Gromm98013452015-09-28 17:18:48 +0200615 snprintf(card->shortname, sizeof(card->shortname), "Microchip MOST:%d",
Christian Gromm54b48562015-07-24 16:11:51 +0200616 card->number);
617 snprintf(card->longname, sizeof(card->longname), "%s at %s, ch %d",
618 card->shortname, iface->description, channel_id);
619
620 ret = snd_pcm_new(card, card_name, 0, playback_count,
621 capture_count, &pcm);
622 if (ret < 0)
623 goto err_free_card;
624
625 pcm->private_data = channel;
626
627 snd_pcm_set_ops(pcm, direction, &pcm_ops);
628
629 ret = snd_card_register(card);
630 if (ret < 0)
631 goto err_free_card;
632
633 list_add_tail(&channel->list, &dev_list);
634
635 return 0;
636
637err_free_card:
638 snd_card_free(card);
639 return ret;
640}
641
642/**
643 * audio_disconnect_channel - function to disconnect a channel
644 * @iface: pointer to interface instance
645 * @channel_id: channel index
646 *
647 * This frees allocated memory and removes the sound card from ALSA
648 *
649 * Returns 0 on success or error code otherwise.
650 */
651static int audio_disconnect_channel(struct most_interface *iface,
652 int channel_id)
653{
654 struct channel *channel;
655
Christian Gromm54b48562015-07-24 16:11:51 +0200656 channel = get_channel(iface, channel_id);
657 if (!channel) {
658 pr_err("sound_disconnect_channel(), invalid channel %d\n",
659 channel_id);
660 return -EINVAL;
661 }
662
663 list_del(&channel->list);
664 snd_card_free(channel->card);
665
666 return 0;
667}
668
669/**
670 * audio_rx_completion - completion handler for rx channels
671 * @mbo: pointer to buffer object that has completed
672 *
673 * This searches for the channel this MBO belongs to and copy the data from MBO
674 * to ring buffer
675 *
676 * Returns 0 on success or error code otherwise.
677 */
678static int audio_rx_completion(struct mbo *mbo)
679{
680 struct channel *channel = get_channel(mbo->ifp, mbo->hdm_channel_id);
681 bool period_elapsed = false;
682
683 if (!channel) {
684 pr_err("sound_rx_completion(), invalid channel %d\n",
685 mbo->hdm_channel_id);
686 return -EINVAL;
687 }
688
689 if (channel->is_stream_running)
690 period_elapsed = copy_data(channel, mbo);
691
692 most_put_mbo(mbo);
693
694 if (period_elapsed)
695 snd_pcm_period_elapsed(channel->substream);
696
697 return 0;
698}
699
700/**
701 * audio_tx_completion - completion handler for tx channels
702 * @iface: pointer to interface instance
703 * @channel_id: channel index/ID
704 *
705 * This searches the channel that belongs to this combination of interface
706 * pointer and channel ID and wakes a process sitting in the wait queue of
707 * this channel.
708 *
709 * Returns 0 on success or error code otherwise.
710 */
711static int audio_tx_completion(struct most_interface *iface, int channel_id)
712{
713 struct channel *channel = get_channel(iface, channel_id);
714
715 if (!channel) {
716 pr_err("sound_tx_completion(), invalid channel %d\n",
717 channel_id);
718 return -EINVAL;
719 }
720
721 wake_up_interruptible(&channel->playback_waitq);
722
723 return 0;
724}
725
726/**
727 * Initialization of the struct most_aim
728 */
729static struct most_aim audio_aim = {
730 .name = DRIVER_NAME,
731 .probe_channel = audio_probe_channel,
732 .disconnect_channel = audio_disconnect_channel,
733 .rx_completion = audio_rx_completion,
734 .tx_completion = audio_tx_completion,
735};
736
737static int __init audio_init(void)
738{
739 pr_info("init()\n");
740
741 INIT_LIST_HEAD(&dev_list);
742
743 return most_register_aim(&audio_aim);
744}
745
746static void __exit audio_exit(void)
747{
748 struct channel *channel, *tmp;
749
750 pr_info("exit()\n");
751
752 list_for_each_entry_safe(channel, tmp, &dev_list, list) {
753 list_del(&channel->list);
754 snd_card_free(channel->card);
755 }
756
757 most_deregister_aim(&audio_aim);
758}
759
760module_init(audio_init);
761module_exit(audio_exit);
762
763MODULE_LICENSE("GPL");
764MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
765MODULE_DESCRIPTION("Audio Application Interface Module for MostCore");