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 rpmsg_msg *msg;
144 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
145 msg = &info->msg[TX_HW_PARAM];
146 msg->s_msg.header.cmd = TX_HW_PARAM;
148 msg = &info->msg[RX_HW_PARAM];
149 msg->s_msg.header.cmd = RX_HW_PARAM;
152 msg->s_msg.param.rate = params_rate(params);
154 switch (params_format(params)) {
155 case SNDRV_PCM_FORMAT_S16_LE:
156 msg->s_msg.param.format = RPMSG_S16_LE;
158 case SNDRV_PCM_FORMAT_S24_LE:
159 msg->s_msg.param.format = RPMSG_S24_LE;
161 case SNDRV_PCM_FORMAT_DSD_U16_LE:
162 msg->s_msg.param.format = RPMSG_DSD_U16_LE;
164 case SNDRV_PCM_FORMAT_DSD_U32_LE:
165 msg->s_msg.param.format = RPMSG_DSD_U32_LE;
168 msg->s_msg.param.format = RPMSG_S32_LE;
172 switch (params_channels(params)) {
174 msg->s_msg.param.channels = RPMSG_CH_LEFT;
177 msg->s_msg.param.channels = RPMSG_CH_STEREO;
180 msg->s_msg.param.channels = params_channels(params);
184 info->send_message(msg, info);
189 static snd_pcm_uframes_t imx_rpmsg_pcm_pointer(struct snd_soc_component *component,
190 struct snd_pcm_substream *substream)
192 struct rpmsg_info *info = dev_get_drvdata(component->dev);
193 struct rpmsg_msg *msg;
194 unsigned int pos = 0;
197 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
198 msg = &info->msg[TX_PERIOD_DONE + MSG_TYPE_A_NUM];
200 msg = &info->msg[RX_PERIOD_DONE + MSG_TYPE_A_NUM];
202 buffer_tail = msg->r_msg.param.buffer_tail;
203 pos = buffer_tail * snd_pcm_lib_period_bytes(substream);
205 return bytes_to_frames(substream->runtime, pos);
208 static void imx_rpmsg_timer_callback(struct timer_list *t)
210 struct stream_timer *stream_timer =
211 from_timer(stream_timer, t, timer);
212 struct snd_pcm_substream *substream = stream_timer->substream;
213 struct rpmsg_info *info = stream_timer->info;
214 struct rpmsg_msg *msg;
216 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
217 msg = &info->msg[TX_PERIOD_DONE + MSG_TYPE_A_NUM];
218 msg->s_msg.header.cmd = TX_PERIOD_DONE;
220 msg = &info->msg[RX_PERIOD_DONE + MSG_TYPE_A_NUM];
221 msg->s_msg.header.cmd = RX_PERIOD_DONE;
224 imx_rpmsg_insert_workqueue(substream, msg, info);
227 static int imx_rpmsg_pcm_open(struct snd_soc_component *component,
228 struct snd_pcm_substream *substream)
230 struct rpmsg_info *info = dev_get_drvdata(component->dev);
231 struct rpmsg_msg *msg;
235 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
236 msg = &info->msg[TX_OPEN];
237 msg->s_msg.header.cmd = TX_OPEN;
239 /* reinitialize buffer counter*/
240 cmd = TX_PERIOD_DONE + MSG_TYPE_A_NUM;
241 info->msg[cmd].s_msg.param.buffer_tail = 0;
242 info->msg[cmd].r_msg.param.buffer_tail = 0;
243 info->msg[TX_POINTER].r_msg.param.buffer_offset = 0;
246 msg = &info->msg[RX_OPEN];
247 msg->s_msg.header.cmd = RX_OPEN;
249 /* reinitialize buffer counter*/
250 cmd = RX_PERIOD_DONE + MSG_TYPE_A_NUM;
251 info->msg[cmd].s_msg.param.buffer_tail = 0;
252 info->msg[cmd].r_msg.param.buffer_tail = 0;
253 info->msg[RX_POINTER].r_msg.param.buffer_offset = 0;
256 info->send_message(msg, info);
258 imx_rpmsg_pcm_hardware.period_bytes_max =
259 imx_rpmsg_pcm_hardware.buffer_bytes_max / 2;
261 snd_soc_set_runtime_hwparams(substream, &imx_rpmsg_pcm_hardware);
263 ret = snd_pcm_hw_constraint_integer(substream->runtime,
264 SNDRV_PCM_HW_PARAM_PERIODS);
268 info->msg_drop_count[substream->stream] = 0;
271 info->stream_timer[substream->stream].info = info;
272 info->stream_timer[substream->stream].substream = substream;
273 timer_setup(&info->stream_timer[substream->stream].timer,
274 imx_rpmsg_timer_callback, 0);
278 static int imx_rpmsg_pcm_close(struct snd_soc_component *component,
279 struct snd_pcm_substream *substream)
281 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
282 struct rpmsg_info *info = dev_get_drvdata(component->dev);
283 struct rpmsg_msg *msg;
285 /* Flush work in workqueue to make TX_CLOSE is the last message */
286 flush_workqueue(info->rpmsg_wq);
288 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
289 msg = &info->msg[TX_CLOSE];
290 msg->s_msg.header.cmd = TX_CLOSE;
292 msg = &info->msg[RX_CLOSE];
293 msg->s_msg.header.cmd = RX_CLOSE;
296 info->send_message(msg, info);
298 del_timer(&info->stream_timer[substream->stream].timer);
300 rtd->dai_link->ignore_suspend = 0;
302 if (info->msg_drop_count[substream->stream])
303 dev_warn(rtd->dev, "Msg is dropped!, number is %d\n",
304 info->msg_drop_count[substream->stream]);
309 static int imx_rpmsg_pcm_prepare(struct snd_soc_component *component,
310 struct snd_pcm_substream *substream)
312 struct snd_pcm_runtime *runtime = substream->runtime;
313 struct snd_soc_pcm_runtime *rtd = substream->private_data;
314 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
315 struct fsl_rpmsg *rpmsg = dev_get_drvdata(cpu_dai->dev);
318 * NON-MMAP mode, NONBLOCK, Version 2, enable lpa in dts
319 * four conditions to determine the lpa is enabled.
321 if ((runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
322 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) &&
325 * Ignore suspend operation in low power mode
326 * M core will continue playback music on A core suspend.
328 rtd->dai_link->ignore_suspend = 1;
329 rpmsg->force_lpa = 1;
331 rpmsg->force_lpa = 0;
337 static void imx_rpmsg_pcm_dma_complete(void *arg)
339 struct snd_pcm_substream *substream = arg;
341 snd_pcm_period_elapsed(substream);
344 static int imx_rpmsg_prepare_and_submit(struct snd_soc_component *component,
345 struct snd_pcm_substream *substream)
347 struct rpmsg_info *info = dev_get_drvdata(component->dev);
348 struct rpmsg_msg *msg;
350 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
351 msg = &info->msg[TX_BUFFER];
352 msg->s_msg.header.cmd = TX_BUFFER;
354 msg = &info->msg[RX_BUFFER];
355 msg->s_msg.header.cmd = RX_BUFFER;
358 /* Send buffer address and buffer size */
359 msg->s_msg.param.buffer_addr = substream->runtime->dma_addr;
360 msg->s_msg.param.buffer_size = snd_pcm_lib_buffer_bytes(substream);
361 msg->s_msg.param.period_size = snd_pcm_lib_period_bytes(substream);
362 msg->s_msg.param.buffer_tail = 0;
364 info->num_period[substream->stream] = msg->s_msg.param.buffer_size /
365 msg->s_msg.param.period_size;
367 info->callback[substream->stream] = imx_rpmsg_pcm_dma_complete;
368 info->callback_param[substream->stream] = substream;
370 return imx_rpmsg_insert_workqueue(substream, msg, info);
373 static int imx_rpmsg_async_issue_pending(struct snd_soc_component *component,
374 struct snd_pcm_substream *substream)
376 struct rpmsg_info *info = dev_get_drvdata(component->dev);
377 struct rpmsg_msg *msg;
379 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
380 msg = &info->msg[TX_START];
381 msg->s_msg.header.cmd = TX_START;
383 msg = &info->msg[RX_START];
384 msg->s_msg.header.cmd = RX_START;
387 return imx_rpmsg_insert_workqueue(substream, msg, info);
390 static int imx_rpmsg_restart(struct snd_soc_component *component,
391 struct snd_pcm_substream *substream)
393 struct rpmsg_info *info = dev_get_drvdata(component->dev);
394 struct rpmsg_msg *msg;
396 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
397 msg = &info->msg[TX_RESTART];
398 msg->s_msg.header.cmd = TX_RESTART;
400 msg = &info->msg[RX_RESTART];
401 msg->s_msg.header.cmd = RX_RESTART;
404 return imx_rpmsg_insert_workqueue(substream, msg, info);
407 static int imx_rpmsg_pause(struct snd_soc_component *component,
408 struct snd_pcm_substream *substream)
410 struct rpmsg_info *info = dev_get_drvdata(component->dev);
411 struct rpmsg_msg *msg;
413 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
414 msg = &info->msg[TX_PAUSE];
415 msg->s_msg.header.cmd = TX_PAUSE;
417 msg = &info->msg[RX_PAUSE];
418 msg->s_msg.header.cmd = RX_PAUSE;
421 return imx_rpmsg_insert_workqueue(substream, msg, info);
424 static int imx_rpmsg_terminate_all(struct snd_soc_component *component,
425 struct snd_pcm_substream *substream)
427 struct rpmsg_info *info = dev_get_drvdata(component->dev);
428 struct rpmsg_msg *msg;
431 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
432 msg = &info->msg[TX_TERMINATE];
433 msg->s_msg.header.cmd = TX_TERMINATE;
434 /* Clear buffer count*/
435 cmd = TX_PERIOD_DONE + MSG_TYPE_A_NUM;
436 info->msg[cmd].s_msg.param.buffer_tail = 0;
437 info->msg[cmd].r_msg.param.buffer_tail = 0;
438 info->msg[TX_POINTER].r_msg.param.buffer_offset = 0;
440 msg = &info->msg[RX_TERMINATE];
441 msg->s_msg.header.cmd = RX_TERMINATE;
442 /* Clear buffer count*/
443 cmd = RX_PERIOD_DONE + MSG_TYPE_A_NUM;
444 info->msg[cmd].s_msg.param.buffer_tail = 0;
445 info->msg[cmd].r_msg.param.buffer_tail = 0;
446 info->msg[RX_POINTER].r_msg.param.buffer_offset = 0;
449 del_timer(&info->stream_timer[substream->stream].timer);
451 return imx_rpmsg_insert_workqueue(substream, msg, info);
454 static int imx_rpmsg_pcm_trigger(struct snd_soc_component *component,
455 struct snd_pcm_substream *substream, int cmd)
457 struct snd_pcm_runtime *runtime = substream->runtime;
458 struct snd_soc_pcm_runtime *rtd = substream->private_data;
459 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
460 struct fsl_rpmsg *rpmsg = dev_get_drvdata(cpu_dai->dev);
464 case SNDRV_PCM_TRIGGER_START:
465 ret = imx_rpmsg_prepare_and_submit(component, substream);
468 ret = imx_rpmsg_async_issue_pending(component, substream);
470 case SNDRV_PCM_TRIGGER_RESUME:
471 if (rpmsg->force_lpa)
474 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
475 ret = imx_rpmsg_restart(component, substream);
477 case SNDRV_PCM_TRIGGER_SUSPEND:
478 if (!rpmsg->force_lpa) {
479 if (runtime->info & SNDRV_PCM_INFO_PAUSE)
480 ret = imx_rpmsg_pause(component, substream);
482 ret = imx_rpmsg_terminate_all(component, substream);
485 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
486 ret = imx_rpmsg_pause(component, substream);
488 case SNDRV_PCM_TRIGGER_STOP:
489 ret = imx_rpmsg_terminate_all(component, substream);
504 * Send the period index to M core through rpmsg, but not send
505 * all the period index to M core, reduce some unnessesary msg
506 * to reduce the pressure of rpmsg bandwidth.
508 static int imx_rpmsg_pcm_ack(struct snd_soc_component *component,
509 struct snd_pcm_substream *substream)
511 struct snd_pcm_runtime *runtime = substream->runtime;
512 struct snd_soc_pcm_runtime *rtd = substream->private_data;
513 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
514 struct fsl_rpmsg *rpmsg = dev_get_drvdata(cpu_dai->dev);
515 struct rpmsg_info *info = dev_get_drvdata(component->dev);
516 snd_pcm_uframes_t period_size = runtime->period_size;
517 snd_pcm_sframes_t avail;
518 struct timer_list *timer;
519 struct rpmsg_msg *msg;
524 if (!rpmsg->force_lpa)
527 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
528 msg = &info->msg[TX_PERIOD_DONE + MSG_TYPE_A_NUM];
529 msg->s_msg.header.cmd = TX_PERIOD_DONE;
531 msg = &info->msg[RX_PERIOD_DONE + MSG_TYPE_A_NUM];
532 msg->s_msg.header.cmd = RX_PERIOD_DONE;
535 msg->s_msg.header.type = MSG_TYPE_C;
537 buffer_tail = (frames_to_bytes(runtime, runtime->control->appl_ptr) %
538 snd_pcm_lib_buffer_bytes(substream));
539 buffer_tail = buffer_tail / snd_pcm_lib_period_bytes(substream);
541 /* There is update for period index */
542 if (buffer_tail != msg->s_msg.param.buffer_tail) {
543 written_num = buffer_tail - msg->s_msg.param.buffer_tail;
545 written_num += runtime->periods;
547 msg->s_msg.param.buffer_tail = buffer_tail;
549 /* The notification message is updated to latest */
550 spin_lock_irqsave(&info->lock[substream->stream], flags);
551 memcpy(&info->notify[substream->stream], msg,
552 sizeof(struct rpmsg_s_msg));
553 info->notify_updated[substream->stream] = true;
554 spin_unlock_irqrestore(&info->lock[substream->stream], flags);
556 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
557 avail = snd_pcm_playback_hw_avail(runtime);
559 avail = snd_pcm_capture_hw_avail(runtime);
561 timer = &info->stream_timer[substream->stream].timer;
563 * If the data in the buffer is less than one period before
564 * this fill, which means the data may not enough on M
565 * core side, we need to send message immediately to let
566 * M core know the pointer is updated.
567 * if there is more than one period data in the buffer before
568 * this fill, which means the data is enough on M core side,
569 * we can delay one period (using timer) to send the message
570 * for reduce the message number in workqueue, because the
571 * pointer may be updated by ack function later, we can
572 * send latest pointer to M core side.
574 if ((avail - written_num * period_size) <= period_size) {
575 imx_rpmsg_insert_workqueue(substream, msg, info);
576 } else if (rpmsg->force_lpa && !timer_pending(timer)) {
579 time_msec = (int)(runtime->period_size * 1000 / runtime->rate);
580 mod_timer(timer, jiffies + msecs_to_jiffies(time_msec));
587 static int imx_rpmsg_pcm_new(struct snd_soc_component *component,
588 struct snd_soc_pcm_runtime *rtd)
590 struct snd_card *card = rtd->card->snd_card;
591 struct snd_pcm *pcm = rtd->pcm;
592 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
593 struct fsl_rpmsg *rpmsg = dev_get_drvdata(cpu_dai->dev);
596 ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
600 imx_rpmsg_pcm_hardware.buffer_bytes_max = rpmsg->buffer_size;
601 return snd_pcm_set_fixed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_WC,
602 pcm->card->dev, rpmsg->buffer_size);
605 static const struct snd_soc_component_driver imx_rpmsg_soc_component = {
606 .name = IMX_PCM_DRV_NAME,
607 .pcm_construct = imx_rpmsg_pcm_new,
608 .open = imx_rpmsg_pcm_open,
609 .close = imx_rpmsg_pcm_close,
610 .hw_params = imx_rpmsg_pcm_hw_params,
611 .trigger = imx_rpmsg_pcm_trigger,
612 .pointer = imx_rpmsg_pcm_pointer,
613 .ack = imx_rpmsg_pcm_ack,
614 .prepare = imx_rpmsg_pcm_prepare,
617 static void imx_rpmsg_pcm_work(struct work_struct *work)
619 struct work_of_rpmsg *work_of_rpmsg;
620 bool is_notification = false;
621 struct rpmsg_info *info;
622 struct rpmsg_msg msg;
625 work_of_rpmsg = container_of(work, struct work_of_rpmsg, work);
626 info = work_of_rpmsg->info;
629 * Every work in the work queue, first we check if there
630 * is update for period is filled, because there may be not
631 * enough data in M core side, need to let M core know
632 * data is updated immediately.
634 spin_lock_irqsave(&info->lock[TX], flags);
635 if (info->notify_updated[TX]) {
636 memcpy(&msg, &info->notify[TX], sizeof(struct rpmsg_s_msg));
637 info->notify_updated[TX] = false;
638 spin_unlock_irqrestore(&info->lock[TX], flags);
639 info->send_message(&msg, info);
641 spin_unlock_irqrestore(&info->lock[TX], flags);
644 spin_lock_irqsave(&info->lock[RX], flags);
645 if (info->notify_updated[RX]) {
646 memcpy(&msg, &info->notify[RX], sizeof(struct rpmsg_s_msg));
647 info->notify_updated[RX] = false;
648 spin_unlock_irqrestore(&info->lock[RX], flags);
649 info->send_message(&msg, info);
651 spin_unlock_irqrestore(&info->lock[RX], flags);
654 /* Skip the notification message for it has been processed above */
655 if (work_of_rpmsg->msg.s_msg.header.type == MSG_TYPE_C &&
656 (work_of_rpmsg->msg.s_msg.header.cmd == TX_PERIOD_DONE ||
657 work_of_rpmsg->msg.s_msg.header.cmd == RX_PERIOD_DONE))
658 is_notification = true;
660 if (!is_notification)
661 info->send_message(&work_of_rpmsg->msg, info);
663 /* update read index */
664 spin_lock_irqsave(&info->wq_lock, flags);
665 info->work_read_index++;
666 info->work_read_index %= WORK_MAX_NUM;
667 spin_unlock_irqrestore(&info->wq_lock, flags);
670 static int imx_rpmsg_pcm_probe(struct platform_device *pdev)
672 struct snd_soc_component *component;
673 struct rpmsg_info *info;
676 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
680 platform_set_drvdata(pdev, info);
682 info->rpdev = container_of(pdev->dev.parent, struct rpmsg_device, dev);
683 info->dev = &pdev->dev;
684 /* Setup work queue */
685 info->rpmsg_wq = alloc_ordered_workqueue(info->rpdev->id.name,
689 if (!info->rpmsg_wq) {
690 dev_err(&pdev->dev, "workqueue create failed\n");
694 /* Write index initialize 1, make it differ with the read index */
695 info->work_write_index = 1;
696 info->send_message = imx_rpmsg_pcm_send_message;
698 for (i = 0; i < WORK_MAX_NUM; i++) {
699 INIT_WORK(&info->work_list[i].work, imx_rpmsg_pcm_work);
700 info->work_list[i].info = info;
704 for (i = 0; i < MSG_MAX_NUM; i++) {
705 info->msg[i].s_msg.header.cate = IMX_RPMSG_AUDIO;
706 info->msg[i].s_msg.header.major = IMX_RMPSG_MAJOR;
707 info->msg[i].s_msg.header.minor = IMX_RMPSG_MINOR;
708 info->msg[i].s_msg.header.type = MSG_TYPE_A;
709 info->msg[i].s_msg.param.audioindex = 0;
712 init_completion(&info->cmd_complete);
713 mutex_init(&info->msg_lock);
714 spin_lock_init(&info->lock[TX]);
715 spin_lock_init(&info->lock[RX]);
716 spin_lock_init(&info->wq_lock);
718 ret = devm_snd_soc_register_component(&pdev->dev,
719 &imx_rpmsg_soc_component,
724 component = snd_soc_lookup_component(&pdev->dev, NULL);
730 /* platform component name is used by machine driver to link with */
731 component->name = info->rpdev->id.name;
733 #ifdef CONFIG_DEBUG_FS
734 component->debugfs_prefix = "rpmsg";
741 destroy_workqueue(info->rpmsg_wq);
746 static void imx_rpmsg_pcm_remove(struct platform_device *pdev)
748 struct rpmsg_info *info = platform_get_drvdata(pdev);
751 destroy_workqueue(info->rpmsg_wq);
755 static int imx_rpmsg_pcm_runtime_resume(struct device *dev)
757 struct rpmsg_info *info = dev_get_drvdata(dev);
759 cpu_latency_qos_add_request(&info->pm_qos_req, 0);
764 static int imx_rpmsg_pcm_runtime_suspend(struct device *dev)
766 struct rpmsg_info *info = dev_get_drvdata(dev);
768 cpu_latency_qos_remove_request(&info->pm_qos_req);
774 #ifdef CONFIG_PM_SLEEP
775 static int imx_rpmsg_pcm_suspend(struct device *dev)
777 struct rpmsg_info *info = dev_get_drvdata(dev);
778 struct rpmsg_msg *rpmsg_tx;
779 struct rpmsg_msg *rpmsg_rx;
781 rpmsg_tx = &info->msg[TX_SUSPEND];
782 rpmsg_rx = &info->msg[RX_SUSPEND];
784 rpmsg_tx->s_msg.header.cmd = TX_SUSPEND;
785 info->send_message(rpmsg_tx, info);
787 rpmsg_rx->s_msg.header.cmd = RX_SUSPEND;
788 info->send_message(rpmsg_rx, info);
793 static int imx_rpmsg_pcm_resume(struct device *dev)
795 struct rpmsg_info *info = dev_get_drvdata(dev);
796 struct rpmsg_msg *rpmsg_tx;
797 struct rpmsg_msg *rpmsg_rx;
799 rpmsg_tx = &info->msg[TX_RESUME];
800 rpmsg_rx = &info->msg[RX_RESUME];
802 rpmsg_tx->s_msg.header.cmd = TX_RESUME;
803 info->send_message(rpmsg_tx, info);
805 rpmsg_rx->s_msg.header.cmd = RX_RESUME;
806 info->send_message(rpmsg_rx, info);
810 #endif /* CONFIG_PM_SLEEP */
812 static const struct dev_pm_ops imx_rpmsg_pcm_pm_ops = {
813 SET_RUNTIME_PM_OPS(imx_rpmsg_pcm_runtime_suspend,
814 imx_rpmsg_pcm_runtime_resume,
816 SET_SYSTEM_SLEEP_PM_OPS(imx_rpmsg_pcm_suspend,
817 imx_rpmsg_pcm_resume)
820 static struct platform_driver imx_pcm_rpmsg_driver = {
821 .probe = imx_rpmsg_pcm_probe,
822 .remove_new = imx_rpmsg_pcm_remove,
824 .name = IMX_PCM_DRV_NAME,
825 .pm = &imx_rpmsg_pcm_pm_ops,
828 module_platform_driver(imx_pcm_rpmsg_driver);
830 MODULE_DESCRIPTION("Freescale SoC Audio RPMSG PCM interface");
831 MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>");
832 MODULE_ALIAS("platform:" IMX_PCM_DRV_NAME);
833 MODULE_LICENSE("GPL v2");