1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017-2021 NXP
4 #include <linux/dma-mapping.h>
5 #include <linux/slab.h>
6 #include <linux/module.h>
7 #include <linux/delay.h>
8 #include <linux/rpmsg.h>
9 #include <sound/core.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm_params.h>
12 #include <sound/dmaengine_pcm.h>
13 #include <sound/soc.h>
16 #include "fsl_rpmsg.h"
17 #include "imx-pcm-rpmsg.h"
19 static struct snd_pcm_hardware imx_rpmsg_pcm_hardware = {
20 .info = SNDRV_PCM_INFO_INTERLEAVED |
21 SNDRV_PCM_INFO_BLOCK_TRANSFER |
23 SNDRV_PCM_INFO_MMAP_VALID |
24 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
25 SNDRV_PCM_INFO_PAUSE |
26 SNDRV_PCM_INFO_RESUME,
27 .buffer_bytes_max = IMX_DEFAULT_DMABUF_SIZE,
28 .period_bytes_min = 512,
29 .period_bytes_max = 65536,
35 static int imx_rpmsg_pcm_send_message(struct rpmsg_msg *msg,
36 struct rpmsg_info *info)
38 struct rpmsg_device *rpdev = info->rpdev;
41 mutex_lock(&info->msg_lock);
43 dev_err(info->dev, "rpmsg channel not ready\n");
44 mutex_unlock(&info->msg_lock);
48 dev_dbg(&rpdev->dev, "send cmd %d\n", msg->s_msg.header.cmd);
50 if (!(msg->s_msg.header.type == MSG_TYPE_C))
51 reinit_completion(&info->cmd_complete);
53 ret = rpmsg_send(rpdev->ept, (void *)&msg->s_msg,
54 sizeof(struct rpmsg_s_msg));
56 dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
57 mutex_unlock(&info->msg_lock);
61 /* No receive msg for TYPE_C command */
62 if (msg->s_msg.header.type == MSG_TYPE_C) {
63 mutex_unlock(&info->msg_lock);
67 /* wait response from rpmsg */
68 ret = wait_for_completion_timeout(&info->cmd_complete,
69 msecs_to_jiffies(RPMSG_TIMEOUT));
71 dev_err(&rpdev->dev, "rpmsg_send cmd %d timeout!\n",
72 msg->s_msg.header.cmd);
73 mutex_unlock(&info->msg_lock);
77 memcpy(&msg->r_msg, &info->r_msg, sizeof(struct rpmsg_r_msg));
78 memcpy(&info->msg[msg->r_msg.header.cmd].r_msg,
79 &msg->r_msg, sizeof(struct rpmsg_r_msg));
82 * Reset the buffer pointer to be zero, actully we have
83 * set the buffer pointer to be zero in imx_rpmsg_terminate_all
84 * But if there is timer task queued in queue, after it is
85 * executed the buffer pointer will be changed, so need to
86 * reset it again with TERMINATE command.
88 switch (msg->s_msg.header.cmd) {
90 info->msg[TX_POINTER].r_msg.param.buffer_offset = 0;
93 info->msg[RX_POINTER].r_msg.param.buffer_offset = 0;
99 dev_dbg(&rpdev->dev, "cmd:%d, resp %d\n", msg->s_msg.header.cmd,
100 info->r_msg.param.resp);
102 mutex_unlock(&info->msg_lock);
107 static int imx_rpmsg_insert_workqueue(struct snd_pcm_substream *substream,
108 struct rpmsg_msg *msg,
109 struct rpmsg_info *info)
115 * Queue the work to workqueue.
116 * If the queue is full, drop the message.
118 spin_lock_irqsave(&info->wq_lock, flags);
119 if (info->work_write_index != info->work_read_index) {
120 int index = info->work_write_index;
122 memcpy(&info->work_list[index].msg, msg,
123 sizeof(struct rpmsg_s_msg));
125 queue_work(info->rpmsg_wq, &info->work_list[index].work);
126 info->work_write_index++;
127 info->work_write_index %= WORK_MAX_NUM;
129 info->msg_drop_count[substream->stream]++;
132 spin_unlock_irqrestore(&info->wq_lock, flags);
137 static int imx_rpmsg_pcm_hw_params(struct snd_soc_component *component,
138 struct snd_pcm_substream *substream,
139 struct snd_pcm_hw_params *params)
141 struct rpmsg_info *info = dev_get_drvdata(component->dev);
142 struct snd_pcm_runtime *runtime = substream->runtime;
143 struct rpmsg_msg *msg;
146 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
147 msg = &info->msg[TX_HW_PARAM];
148 msg->s_msg.header.cmd = TX_HW_PARAM;
150 msg = &info->msg[RX_HW_PARAM];
151 msg->s_msg.header.cmd = RX_HW_PARAM;
154 msg->s_msg.param.rate = params_rate(params);
156 switch (params_format(params)) {
157 case SNDRV_PCM_FORMAT_S16_LE:
158 msg->s_msg.param.format = RPMSG_S16_LE;
160 case SNDRV_PCM_FORMAT_S24_LE:
161 msg->s_msg.param.format = RPMSG_S24_LE;
163 case SNDRV_PCM_FORMAT_DSD_U16_LE:
164 msg->s_msg.param.format = SNDRV_PCM_FORMAT_DSD_U16_LE;
166 case SNDRV_PCM_FORMAT_DSD_U32_LE:
167 msg->s_msg.param.format = SNDRV_PCM_FORMAT_DSD_U32_LE;
170 msg->s_msg.param.format = RPMSG_S32_LE;
174 switch (params_channels(params)) {
176 msg->s_msg.param.channels = RPMSG_CH_LEFT;
179 msg->s_msg.param.channels = RPMSG_CH_STEREO;
186 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
187 runtime->dma_bytes = params_buffer_bytes(params);
189 info->send_message(msg, info);
194 static int imx_rpmsg_pcm_hw_free(struct snd_soc_component *component,
195 struct snd_pcm_substream *substream)
197 snd_pcm_set_runtime_buffer(substream, NULL);
201 static snd_pcm_uframes_t imx_rpmsg_pcm_pointer(struct snd_soc_component *component,
202 struct snd_pcm_substream *substream)
204 struct rpmsg_info *info = dev_get_drvdata(component->dev);
205 struct rpmsg_msg *msg;
206 unsigned int pos = 0;
209 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
210 msg = &info->msg[TX_PERIOD_DONE + MSG_TYPE_A_NUM];
212 msg = &info->msg[RX_PERIOD_DONE + MSG_TYPE_A_NUM];
214 buffer_tail = msg->r_msg.param.buffer_tail;
215 pos = buffer_tail * snd_pcm_lib_period_bytes(substream);
217 return bytes_to_frames(substream->runtime, pos);
220 static void imx_rpmsg_timer_callback(struct timer_list *t)
222 struct stream_timer *stream_timer =
223 from_timer(stream_timer, t, timer);
224 struct snd_pcm_substream *substream = stream_timer->substream;
225 struct rpmsg_info *info = stream_timer->info;
226 struct rpmsg_msg *msg;
228 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
229 msg = &info->msg[TX_PERIOD_DONE + MSG_TYPE_A_NUM];
230 msg->s_msg.header.cmd = TX_PERIOD_DONE;
232 msg = &info->msg[RX_PERIOD_DONE + MSG_TYPE_A_NUM];
233 msg->s_msg.header.cmd = RX_PERIOD_DONE;
236 imx_rpmsg_insert_workqueue(substream, msg, info);
239 static int imx_rpmsg_pcm_open(struct snd_soc_component *component,
240 struct snd_pcm_substream *substream)
242 struct rpmsg_info *info = dev_get_drvdata(component->dev);
243 struct rpmsg_msg *msg;
247 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
248 msg = &info->msg[TX_OPEN];
249 msg->s_msg.header.cmd = TX_OPEN;
251 /* reinitialize buffer counter*/
252 cmd = TX_PERIOD_DONE + MSG_TYPE_A_NUM;
253 info->msg[cmd].s_msg.param.buffer_tail = 0;
254 info->msg[cmd].r_msg.param.buffer_tail = 0;
255 info->msg[TX_POINTER].r_msg.param.buffer_offset = 0;
258 msg = &info->msg[RX_OPEN];
259 msg->s_msg.header.cmd = RX_OPEN;
261 /* reinitialize buffer counter*/
262 cmd = RX_PERIOD_DONE + MSG_TYPE_A_NUM;
263 info->msg[cmd].s_msg.param.buffer_tail = 0;
264 info->msg[cmd].r_msg.param.buffer_tail = 0;
265 info->msg[RX_POINTER].r_msg.param.buffer_offset = 0;
268 info->send_message(msg, info);
270 imx_rpmsg_pcm_hardware.period_bytes_max =
271 imx_rpmsg_pcm_hardware.buffer_bytes_max / 2;
273 snd_soc_set_runtime_hwparams(substream, &imx_rpmsg_pcm_hardware);
275 ret = snd_pcm_hw_constraint_integer(substream->runtime,
276 SNDRV_PCM_HW_PARAM_PERIODS);
280 info->msg_drop_count[substream->stream] = 0;
283 info->stream_timer[substream->stream].info = info;
284 info->stream_timer[substream->stream].substream = substream;
285 timer_setup(&info->stream_timer[substream->stream].timer,
286 imx_rpmsg_timer_callback, 0);
290 static int imx_rpmsg_pcm_close(struct snd_soc_component *component,
291 struct snd_pcm_substream *substream)
293 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
294 struct rpmsg_info *info = dev_get_drvdata(component->dev);
295 struct rpmsg_msg *msg;
298 /* Flush work in workqueue to make TX_CLOSE is the last message */
299 flush_workqueue(info->rpmsg_wq);
301 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
302 msg = &info->msg[TX_CLOSE];
303 msg->s_msg.header.cmd = TX_CLOSE;
305 msg = &info->msg[RX_CLOSE];
306 msg->s_msg.header.cmd = RX_CLOSE;
309 info->send_message(msg, info);
311 del_timer(&info->stream_timer[substream->stream].timer);
313 rtd->dai_link->ignore_suspend = 0;
315 if (info->msg_drop_count[substream->stream])
316 dev_warn(rtd->dev, "Msg is dropped!, number is %d\n",
317 info->msg_drop_count[substream->stream]);
322 static int imx_rpmsg_pcm_prepare(struct snd_soc_component *component,
323 struct snd_pcm_substream *substream)
325 struct snd_pcm_runtime *runtime = substream->runtime;
326 struct snd_soc_pcm_runtime *rtd = substream->private_data;
327 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
328 struct fsl_rpmsg *rpmsg = dev_get_drvdata(cpu_dai->dev);
331 * NON-MMAP mode, NONBLOCK, Version 2, enable lpa in dts
332 * four conditions to determine the lpa is enabled.
334 if ((runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
335 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) &&
338 * Ignore suspend operation in low power mode
339 * M core will continue playback music on A core suspend.
341 rtd->dai_link->ignore_suspend = 1;
342 rpmsg->force_lpa = 1;
344 rpmsg->force_lpa = 0;
350 static int imx_rpmsg_pcm_mmap(struct snd_soc_component *component,
351 struct snd_pcm_substream *substream,
352 struct vm_area_struct *vma)
354 struct snd_pcm_runtime *runtime = substream->runtime;
356 return dma_mmap_wc(substream->pcm->card->dev, vma,
362 static void imx_rpmsg_pcm_dma_complete(void *arg)
364 struct snd_pcm_substream *substream = arg;
366 snd_pcm_period_elapsed(substream);
369 static int imx_rpmsg_prepare_and_submit(struct snd_soc_component *component,
370 struct snd_pcm_substream *substream)
372 struct rpmsg_info *info = dev_get_drvdata(component->dev);
373 struct rpmsg_msg *msg;
375 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
376 msg = &info->msg[TX_BUFFER];
377 msg->s_msg.header.cmd = TX_BUFFER;
379 msg = &info->msg[RX_BUFFER];
380 msg->s_msg.header.cmd = RX_BUFFER;
383 /* Send buffer address and buffer size */
384 msg->s_msg.param.buffer_addr = substream->runtime->dma_addr;
385 msg->s_msg.param.buffer_size = snd_pcm_lib_buffer_bytes(substream);
386 msg->s_msg.param.period_size = snd_pcm_lib_period_bytes(substream);
387 msg->s_msg.param.buffer_tail = 0;
389 info->num_period[substream->stream] = msg->s_msg.param.buffer_size /
390 msg->s_msg.param.period_size;
392 info->callback[substream->stream] = imx_rpmsg_pcm_dma_complete;
393 info->callback_param[substream->stream] = substream;
395 return imx_rpmsg_insert_workqueue(substream, msg, info);
398 static int imx_rpmsg_async_issue_pending(struct snd_soc_component *component,
399 struct snd_pcm_substream *substream)
401 struct rpmsg_info *info = dev_get_drvdata(component->dev);
402 struct rpmsg_msg *msg;
404 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
405 msg = &info->msg[TX_START];
406 msg->s_msg.header.cmd = TX_START;
408 msg = &info->msg[RX_START];
409 msg->s_msg.header.cmd = RX_START;
412 return imx_rpmsg_insert_workqueue(substream, msg, info);
415 static int imx_rpmsg_restart(struct snd_soc_component *component,
416 struct snd_pcm_substream *substream)
418 struct rpmsg_info *info = dev_get_drvdata(component->dev);
419 struct rpmsg_msg *msg;
421 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
422 msg = &info->msg[TX_RESTART];
423 msg->s_msg.header.cmd = TX_RESTART;
425 msg = &info->msg[RX_RESTART];
426 msg->s_msg.header.cmd = RX_RESTART;
429 return imx_rpmsg_insert_workqueue(substream, msg, info);
432 static int imx_rpmsg_pause(struct snd_soc_component *component,
433 struct snd_pcm_substream *substream)
435 struct rpmsg_info *info = dev_get_drvdata(component->dev);
436 struct rpmsg_msg *msg;
438 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
439 msg = &info->msg[TX_PAUSE];
440 msg->s_msg.header.cmd = TX_PAUSE;
442 msg = &info->msg[RX_PAUSE];
443 msg->s_msg.header.cmd = RX_PAUSE;
446 return imx_rpmsg_insert_workqueue(substream, msg, info);
449 static int imx_rpmsg_terminate_all(struct snd_soc_component *component,
450 struct snd_pcm_substream *substream)
452 struct rpmsg_info *info = dev_get_drvdata(component->dev);
453 struct rpmsg_msg *msg;
456 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
457 msg = &info->msg[TX_TERMINATE];
458 msg->s_msg.header.cmd = TX_TERMINATE;
459 /* Clear buffer count*/
460 cmd = TX_PERIOD_DONE + MSG_TYPE_A_NUM;
461 info->msg[cmd].s_msg.param.buffer_tail = 0;
462 info->msg[cmd].r_msg.param.buffer_tail = 0;
463 info->msg[TX_POINTER].r_msg.param.buffer_offset = 0;
465 msg = &info->msg[RX_TERMINATE];
466 msg->s_msg.header.cmd = RX_TERMINATE;
467 /* Clear buffer count*/
468 cmd = RX_PERIOD_DONE + MSG_TYPE_A_NUM;
469 info->msg[cmd].s_msg.param.buffer_tail = 0;
470 info->msg[cmd].r_msg.param.buffer_tail = 0;
471 info->msg[RX_POINTER].r_msg.param.buffer_offset = 0;
474 del_timer(&info->stream_timer[substream->stream].timer);
476 return imx_rpmsg_insert_workqueue(substream, msg, info);
479 static int imx_rpmsg_pcm_trigger(struct snd_soc_component *component,
480 struct snd_pcm_substream *substream, int cmd)
482 struct snd_pcm_runtime *runtime = substream->runtime;
483 struct snd_soc_pcm_runtime *rtd = substream->private_data;
484 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
485 struct fsl_rpmsg *rpmsg = dev_get_drvdata(cpu_dai->dev);
489 case SNDRV_PCM_TRIGGER_START:
490 ret = imx_rpmsg_prepare_and_submit(component, substream);
493 ret = imx_rpmsg_async_issue_pending(component, substream);
495 case SNDRV_PCM_TRIGGER_RESUME:
496 if (rpmsg->force_lpa)
499 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
500 ret = imx_rpmsg_restart(component, substream);
502 case SNDRV_PCM_TRIGGER_SUSPEND:
503 if (!rpmsg->force_lpa) {
504 if (runtime->info & SNDRV_PCM_INFO_PAUSE)
505 ret = imx_rpmsg_pause(component, substream);
507 ret = imx_rpmsg_terminate_all(component, substream);
510 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
511 ret = imx_rpmsg_pause(component, substream);
513 case SNDRV_PCM_TRIGGER_STOP:
514 ret = imx_rpmsg_terminate_all(component, substream);
529 * Send the period index to M core through rpmsg, but not send
530 * all the period index to M core, reduce some unnessesary msg
531 * to reduce the pressure of rpmsg bandwidth.
533 static int imx_rpmsg_pcm_ack(struct snd_soc_component *component,
534 struct snd_pcm_substream *substream)
536 struct snd_pcm_runtime *runtime = substream->runtime;
537 struct snd_soc_pcm_runtime *rtd = substream->private_data;
538 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
539 struct fsl_rpmsg *rpmsg = dev_get_drvdata(cpu_dai->dev);
540 struct rpmsg_info *info = dev_get_drvdata(component->dev);
541 snd_pcm_uframes_t period_size = runtime->period_size;
542 snd_pcm_sframes_t avail;
543 struct timer_list *timer;
544 struct rpmsg_msg *msg;
549 if (!rpmsg->force_lpa)
552 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
553 msg = &info->msg[TX_PERIOD_DONE + MSG_TYPE_A_NUM];
554 msg->s_msg.header.cmd = TX_PERIOD_DONE;
556 msg = &info->msg[RX_PERIOD_DONE + MSG_TYPE_A_NUM];
557 msg->s_msg.header.cmd = RX_PERIOD_DONE;
560 msg->s_msg.header.type = MSG_TYPE_C;
562 buffer_tail = (frames_to_bytes(runtime, runtime->control->appl_ptr) %
563 snd_pcm_lib_buffer_bytes(substream));
564 buffer_tail = buffer_tail / snd_pcm_lib_period_bytes(substream);
566 /* There is update for period index */
567 if (buffer_tail != msg->s_msg.param.buffer_tail) {
568 written_num = buffer_tail - msg->s_msg.param.buffer_tail;
570 written_num += runtime->periods;
572 msg->s_msg.param.buffer_tail = buffer_tail;
574 /* The notification message is updated to latest */
575 spin_lock_irqsave(&info->lock[substream->stream], flags);
576 memcpy(&info->notify[substream->stream], msg,
577 sizeof(struct rpmsg_s_msg));
578 info->notify_updated[substream->stream] = true;
579 spin_unlock_irqrestore(&info->lock[substream->stream], flags);
581 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
582 avail = snd_pcm_playback_hw_avail(runtime);
584 avail = snd_pcm_capture_hw_avail(runtime);
586 timer = &info->stream_timer[substream->stream].timer;
588 * If the data in the buffer is less than one period before
589 * this fill, which means the data may not enough on M
590 * core side, we need to send message immediately to let
591 * M core know the pointer is updated.
592 * if there is more than one period data in the buffer before
593 * this fill, which means the data is enough on M core side,
594 * we can delay one period (using timer) to send the message
595 * for reduce the message number in workqueue, because the
596 * pointer may be updated by ack function later, we can
597 * send latest pointer to M core side.
599 if ((avail - written_num * period_size) <= period_size) {
600 imx_rpmsg_insert_workqueue(substream, msg, info);
601 } else if (rpmsg->force_lpa && !timer_pending(timer)) {
604 time_msec = (int)(runtime->period_size * 1000 / runtime->rate);
605 mod_timer(timer, jiffies + msecs_to_jiffies(time_msec));
612 static int imx_rpmsg_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
613 int stream, int size)
615 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
616 struct snd_dma_buffer *buf = &substream->dma_buffer;
618 buf->dev.type = SNDRV_DMA_TYPE_DEV;
619 buf->dev.dev = pcm->card->dev;
620 buf->private_data = NULL;
621 buf->area = dma_alloc_wc(pcm->card->dev, size,
622 &buf->addr, GFP_KERNEL);
630 static void imx_rpmsg_pcm_free_dma_buffers(struct snd_soc_component *component,
633 struct snd_pcm_substream *substream;
634 struct snd_dma_buffer *buf;
637 for (stream = SNDRV_PCM_STREAM_PLAYBACK;
638 stream < SNDRV_PCM_STREAM_LAST; stream++) {
639 substream = pcm->streams[stream].substream;
643 buf = &substream->dma_buffer;
647 dma_free_wc(pcm->card->dev, buf->bytes,
648 buf->area, buf->addr);
653 static int imx_rpmsg_pcm_new(struct snd_soc_component *component,
654 struct snd_soc_pcm_runtime *rtd)
656 struct snd_card *card = rtd->card->snd_card;
657 struct snd_pcm *pcm = rtd->pcm;
658 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
659 struct fsl_rpmsg *rpmsg = dev_get_drvdata(cpu_dai->dev);
662 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
666 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
667 ret = imx_rpmsg_pcm_preallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK,
673 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
674 ret = imx_rpmsg_pcm_preallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_CAPTURE,
680 imx_rpmsg_pcm_hardware.buffer_bytes_max = rpmsg->buffer_size;
682 /* free preallocated buffers in case of error */
684 imx_rpmsg_pcm_free_dma_buffers(component, pcm);
689 static const struct snd_soc_component_driver imx_rpmsg_soc_component = {
690 .name = IMX_PCM_DRV_NAME,
691 .pcm_construct = imx_rpmsg_pcm_new,
692 .pcm_destruct = imx_rpmsg_pcm_free_dma_buffers,
693 .open = imx_rpmsg_pcm_open,
694 .close = imx_rpmsg_pcm_close,
695 .hw_params = imx_rpmsg_pcm_hw_params,
696 .hw_free = imx_rpmsg_pcm_hw_free,
697 .trigger = imx_rpmsg_pcm_trigger,
698 .pointer = imx_rpmsg_pcm_pointer,
699 .mmap = imx_rpmsg_pcm_mmap,
700 .ack = imx_rpmsg_pcm_ack,
701 .prepare = imx_rpmsg_pcm_prepare,
704 static void imx_rpmsg_pcm_work(struct work_struct *work)
706 struct work_of_rpmsg *work_of_rpmsg;
707 bool is_notification = false;
708 struct rpmsg_info *info;
709 struct rpmsg_msg msg;
712 work_of_rpmsg = container_of(work, struct work_of_rpmsg, work);
713 info = work_of_rpmsg->info;
716 * Every work in the work queue, first we check if there
717 * is update for period is filled, because there may be not
718 * enough data in M core side, need to let M core know
719 * data is updated immediately.
721 spin_lock_irqsave(&info->lock[TX], flags);
722 if (info->notify_updated[TX]) {
723 memcpy(&msg, &info->notify[TX], sizeof(struct rpmsg_s_msg));
724 info->notify_updated[TX] = false;
725 spin_unlock_irqrestore(&info->lock[TX], flags);
726 info->send_message(&msg, info);
728 spin_unlock_irqrestore(&info->lock[TX], flags);
731 spin_lock_irqsave(&info->lock[RX], flags);
732 if (info->notify_updated[RX]) {
733 memcpy(&msg, &info->notify[RX], sizeof(struct rpmsg_s_msg));
734 info->notify_updated[RX] = false;
735 spin_unlock_irqrestore(&info->lock[RX], flags);
736 info->send_message(&msg, info);
738 spin_unlock_irqrestore(&info->lock[RX], flags);
741 /* Skip the notification message for it has been processed above */
742 if (work_of_rpmsg->msg.s_msg.header.type == MSG_TYPE_C &&
743 (work_of_rpmsg->msg.s_msg.header.cmd == TX_PERIOD_DONE ||
744 work_of_rpmsg->msg.s_msg.header.cmd == RX_PERIOD_DONE))
745 is_notification = true;
747 if (!is_notification)
748 info->send_message(&work_of_rpmsg->msg, info);
750 /* update read index */
751 spin_lock_irqsave(&info->wq_lock, flags);
752 info->work_read_index++;
753 info->work_read_index %= WORK_MAX_NUM;
754 spin_unlock_irqrestore(&info->wq_lock, flags);
757 static int imx_rpmsg_pcm_probe(struct platform_device *pdev)
759 struct snd_soc_component *component;
760 struct rpmsg_info *info;
763 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
767 platform_set_drvdata(pdev, info);
769 info->rpdev = container_of(pdev->dev.parent, struct rpmsg_device, dev);
770 info->dev = &pdev->dev;
771 /* Setup work queue */
772 info->rpmsg_wq = alloc_ordered_workqueue("rpmsg_audio",
776 if (!info->rpmsg_wq) {
777 dev_err(&pdev->dev, "workqueue create failed\n");
781 /* Write index initialize 1, make it differ with the read index */
782 info->work_write_index = 1;
783 info->send_message = imx_rpmsg_pcm_send_message;
785 for (i = 0; i < WORK_MAX_NUM; i++) {
786 INIT_WORK(&info->work_list[i].work, imx_rpmsg_pcm_work);
787 info->work_list[i].info = info;
791 for (i = 0; i < MSG_MAX_NUM; i++) {
792 info->msg[i].s_msg.header.cate = IMX_RPMSG_AUDIO;
793 info->msg[i].s_msg.header.major = IMX_RMPSG_MAJOR;
794 info->msg[i].s_msg.header.minor = IMX_RMPSG_MINOR;
795 info->msg[i].s_msg.header.type = MSG_TYPE_A;
796 info->msg[i].s_msg.param.audioindex = 0;
799 init_completion(&info->cmd_complete);
800 mutex_init(&info->msg_lock);
801 spin_lock_init(&info->lock[TX]);
802 spin_lock_init(&info->lock[RX]);
803 spin_lock_init(&info->wq_lock);
805 ret = devm_snd_soc_register_component(&pdev->dev,
806 &imx_rpmsg_soc_component,
811 component = snd_soc_lookup_component(&pdev->dev, IMX_PCM_DRV_NAME);
816 #ifdef CONFIG_DEBUG_FS
817 component->debugfs_prefix = "rpmsg";
824 destroy_workqueue(info->rpmsg_wq);
829 static int imx_rpmsg_pcm_remove(struct platform_device *pdev)
831 struct rpmsg_info *info = platform_get_drvdata(pdev);
834 destroy_workqueue(info->rpmsg_wq);
840 static int imx_rpmsg_pcm_runtime_resume(struct device *dev)
842 struct rpmsg_info *info = dev_get_drvdata(dev);
844 cpu_latency_qos_add_request(&info->pm_qos_req, 0);
849 static int imx_rpmsg_pcm_runtime_suspend(struct device *dev)
851 struct rpmsg_info *info = dev_get_drvdata(dev);
853 cpu_latency_qos_remove_request(&info->pm_qos_req);
859 #ifdef CONFIG_PM_SLEEP
860 static int imx_rpmsg_pcm_suspend(struct device *dev)
862 struct rpmsg_info *info = dev_get_drvdata(dev);
863 struct rpmsg_msg *rpmsg_tx;
864 struct rpmsg_msg *rpmsg_rx;
866 rpmsg_tx = &info->msg[TX_SUSPEND];
867 rpmsg_rx = &info->msg[RX_SUSPEND];
869 rpmsg_tx->s_msg.header.cmd = TX_SUSPEND;
870 info->send_message(rpmsg_tx, info);
872 rpmsg_rx->s_msg.header.cmd = RX_SUSPEND;
873 info->send_message(rpmsg_rx, info);
878 static int imx_rpmsg_pcm_resume(struct device *dev)
880 struct rpmsg_info *info = dev_get_drvdata(dev);
881 struct rpmsg_msg *rpmsg_tx;
882 struct rpmsg_msg *rpmsg_rx;
884 rpmsg_tx = &info->msg[TX_RESUME];
885 rpmsg_rx = &info->msg[RX_RESUME];
887 rpmsg_tx->s_msg.header.cmd = TX_RESUME;
888 info->send_message(rpmsg_tx, info);
890 rpmsg_rx->s_msg.header.cmd = RX_RESUME;
891 info->send_message(rpmsg_rx, info);
895 #endif /* CONFIG_PM_SLEEP */
897 static const struct dev_pm_ops imx_rpmsg_pcm_pm_ops = {
898 SET_RUNTIME_PM_OPS(imx_rpmsg_pcm_runtime_suspend,
899 imx_rpmsg_pcm_runtime_resume,
901 SET_SYSTEM_SLEEP_PM_OPS(imx_rpmsg_pcm_suspend,
902 imx_rpmsg_pcm_resume)
905 static struct platform_driver imx_pcm_rpmsg_driver = {
906 .probe = imx_rpmsg_pcm_probe,
907 .remove = imx_rpmsg_pcm_remove,
909 .name = IMX_PCM_DRV_NAME,
910 .pm = &imx_rpmsg_pcm_pm_ops,
913 module_platform_driver(imx_pcm_rpmsg_driver);
915 MODULE_DESCRIPTION("Freescale SoC Audio RPMSG PCM interface");
916 MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>");
917 MODULE_ALIAS("platform:" IMX_PCM_DRV_NAME);
918 MODULE_LICENSE("GPL v2");