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 const struct snd_pcm_hardware imx_rpmsg_pcm_hardware
= {
20 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
21 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
22 SNDRV_PCM_INFO_BATCH
|
24 SNDRV_PCM_INFO_MMAP_VALID
|
25 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
|
26 SNDRV_PCM_INFO_PAUSE
|
27 SNDRV_PCM_INFO_RESUME
,
28 .buffer_bytes_max
= IMX_DEFAULT_DMABUF_SIZE
,
29 .period_bytes_min
= 512,
30 .period_bytes_max
= 65536,
36 static int imx_rpmsg_pcm_send_message(struct rpmsg_msg
*msg
,
37 struct rpmsg_info
*info
)
39 struct rpmsg_device
*rpdev
= info
->rpdev
;
42 mutex_lock(&info
->msg_lock
);
44 dev_err(info
->dev
, "rpmsg channel not ready\n");
45 mutex_unlock(&info
->msg_lock
);
49 dev_dbg(&rpdev
->dev
, "send cmd %d\n", msg
->s_msg
.header
.cmd
);
51 if (!(msg
->s_msg
.header
.type
== MSG_TYPE_C
))
52 reinit_completion(&info
->cmd_complete
);
54 ret
= rpmsg_send(rpdev
->ept
, (void *)&msg
->s_msg
,
55 sizeof(struct rpmsg_s_msg
));
57 dev_err(&rpdev
->dev
, "rpmsg_send failed: %d\n", ret
);
58 mutex_unlock(&info
->msg_lock
);
62 /* No receive msg for TYPE_C command */
63 if (msg
->s_msg
.header
.type
== MSG_TYPE_C
) {
64 mutex_unlock(&info
->msg_lock
);
68 /* wait response from rpmsg */
69 ret
= wait_for_completion_timeout(&info
->cmd_complete
,
70 msecs_to_jiffies(RPMSG_TIMEOUT
));
72 dev_err(&rpdev
->dev
, "rpmsg_send cmd %d timeout!\n",
73 msg
->s_msg
.header
.cmd
);
74 mutex_unlock(&info
->msg_lock
);
78 memcpy(&msg
->r_msg
, &info
->r_msg
, sizeof(struct rpmsg_r_msg
));
79 memcpy(&info
->msg
[msg
->r_msg
.header
.cmd
].r_msg
,
80 &msg
->r_msg
, sizeof(struct rpmsg_r_msg
));
83 * Reset the buffer pointer to be zero, actully we have
84 * set the buffer pointer to be zero in imx_rpmsg_terminate_all
85 * But if there is timer task queued in queue, after it is
86 * executed the buffer pointer will be changed, so need to
87 * reset it again with TERMINATE command.
89 switch (msg
->s_msg
.header
.cmd
) {
91 info
->msg
[TX_POINTER
].r_msg
.param
.buffer_offset
= 0;
94 info
->msg
[RX_POINTER
].r_msg
.param
.buffer_offset
= 0;
100 dev_dbg(&rpdev
->dev
, "cmd:%d, resp %d\n", msg
->s_msg
.header
.cmd
,
101 info
->r_msg
.param
.resp
);
103 mutex_unlock(&info
->msg_lock
);
108 static int imx_rpmsg_insert_workqueue(struct snd_pcm_substream
*substream
,
109 struct rpmsg_msg
*msg
,
110 struct rpmsg_info
*info
)
116 * Queue the work to workqueue.
117 * If the queue is full, drop the message.
119 spin_lock_irqsave(&info
->wq_lock
, flags
);
120 if (info
->work_write_index
!= info
->work_read_index
) {
121 int index
= info
->work_write_index
;
123 memcpy(&info
->work_list
[index
].msg
, msg
,
124 sizeof(struct rpmsg_s_msg
));
126 queue_work(info
->rpmsg_wq
, &info
->work_list
[index
].work
);
127 info
->work_write_index
++;
128 info
->work_write_index
%= WORK_MAX_NUM
;
130 info
->msg_drop_count
[substream
->stream
]++;
133 spin_unlock_irqrestore(&info
->wq_lock
, flags
);
138 static int imx_rpmsg_pcm_hw_params(struct snd_soc_component
*component
,
139 struct snd_pcm_substream
*substream
,
140 struct snd_pcm_hw_params
*params
)
142 struct rpmsg_info
*info
= dev_get_drvdata(component
->dev
);
143 struct rpmsg_msg
*msg
;
145 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
146 msg
= &info
->msg
[TX_HW_PARAM
];
147 msg
->s_msg
.header
.cmd
= TX_HW_PARAM
;
149 msg
= &info
->msg
[RX_HW_PARAM
];
150 msg
->s_msg
.header
.cmd
= RX_HW_PARAM
;
153 msg
->s_msg
.param
.rate
= params_rate(params
);
155 switch (params_format(params
)) {
156 case SNDRV_PCM_FORMAT_S16_LE
:
157 msg
->s_msg
.param
.format
= RPMSG_S16_LE
;
159 case SNDRV_PCM_FORMAT_S24_LE
:
160 msg
->s_msg
.param
.format
= RPMSG_S24_LE
;
162 case SNDRV_PCM_FORMAT_DSD_U16_LE
:
163 msg
->s_msg
.param
.format
= RPMSG_DSD_U16_LE
;
165 case SNDRV_PCM_FORMAT_DSD_U32_LE
:
166 msg
->s_msg
.param
.format
= RPMSG_DSD_U32_LE
;
169 msg
->s_msg
.param
.format
= RPMSG_S32_LE
;
173 switch (params_channels(params
)) {
175 msg
->s_msg
.param
.channels
= RPMSG_CH_LEFT
;
178 msg
->s_msg
.param
.channels
= RPMSG_CH_STEREO
;
181 msg
->s_msg
.param
.channels
= params_channels(params
);
185 info
->send_message(msg
, info
);
190 static snd_pcm_uframes_t
imx_rpmsg_pcm_pointer(struct snd_soc_component
*component
,
191 struct snd_pcm_substream
*substream
)
193 struct rpmsg_info
*info
= dev_get_drvdata(component
->dev
);
194 struct rpmsg_msg
*msg
;
195 unsigned int pos
= 0;
198 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
199 msg
= &info
->msg
[TX_PERIOD_DONE
+ MSG_TYPE_A_NUM
];
201 msg
= &info
->msg
[RX_PERIOD_DONE
+ MSG_TYPE_A_NUM
];
203 buffer_tail
= msg
->r_msg
.param
.buffer_tail
;
204 pos
= buffer_tail
* snd_pcm_lib_period_bytes(substream
);
206 return bytes_to_frames(substream
->runtime
, pos
);
209 static void imx_rpmsg_timer_callback(struct timer_list
*t
)
211 struct stream_timer
*stream_timer
=
212 from_timer(stream_timer
, t
, timer
);
213 struct snd_pcm_substream
*substream
= stream_timer
->substream
;
214 struct rpmsg_info
*info
= stream_timer
->info
;
215 struct rpmsg_msg
*msg
;
217 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
218 msg
= &info
->msg
[TX_PERIOD_DONE
+ MSG_TYPE_A_NUM
];
219 msg
->s_msg
.header
.cmd
= TX_PERIOD_DONE
;
221 msg
= &info
->msg
[RX_PERIOD_DONE
+ MSG_TYPE_A_NUM
];
222 msg
->s_msg
.header
.cmd
= RX_PERIOD_DONE
;
225 imx_rpmsg_insert_workqueue(substream
, msg
, info
);
228 static int imx_rpmsg_pcm_open(struct snd_soc_component
*component
,
229 struct snd_pcm_substream
*substream
)
231 struct rpmsg_info
*info
= dev_get_drvdata(component
->dev
);
232 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
233 struct snd_soc_dai
*cpu_dai
= snd_soc_rtd_to_cpu(rtd
, 0);
234 struct fsl_rpmsg
*rpmsg
= dev_get_drvdata(cpu_dai
->dev
);
235 struct snd_pcm_hardware pcm_hardware
;
236 struct rpmsg_msg
*msg
;
240 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
241 msg
= &info
->msg
[TX_OPEN
];
242 msg
->s_msg
.header
.cmd
= TX_OPEN
;
244 /* reinitialize buffer counter*/
245 cmd
= TX_PERIOD_DONE
+ MSG_TYPE_A_NUM
;
246 info
->msg
[cmd
].s_msg
.param
.buffer_tail
= 0;
247 info
->msg
[cmd
].r_msg
.param
.buffer_tail
= 0;
248 info
->msg
[TX_POINTER
].r_msg
.param
.buffer_offset
= 0;
251 msg
= &info
->msg
[RX_OPEN
];
252 msg
->s_msg
.header
.cmd
= RX_OPEN
;
254 /* reinitialize buffer counter*/
255 cmd
= RX_PERIOD_DONE
+ MSG_TYPE_A_NUM
;
256 info
->msg
[cmd
].s_msg
.param
.buffer_tail
= 0;
257 info
->msg
[cmd
].r_msg
.param
.buffer_tail
= 0;
258 info
->msg
[RX_POINTER
].r_msg
.param
.buffer_offset
= 0;
261 info
->send_message(msg
, info
);
263 pcm_hardware
= imx_rpmsg_pcm_hardware
;
264 pcm_hardware
.buffer_bytes_max
= rpmsg
->buffer_size
;
265 pcm_hardware
.period_bytes_max
= pcm_hardware
.buffer_bytes_max
/ 2;
267 snd_soc_set_runtime_hwparams(substream
, &pcm_hardware
);
269 ret
= snd_pcm_hw_constraint_integer(substream
->runtime
,
270 SNDRV_PCM_HW_PARAM_PERIODS
);
274 info
->msg_drop_count
[substream
->stream
] = 0;
277 info
->stream_timer
[substream
->stream
].info
= info
;
278 info
->stream_timer
[substream
->stream
].substream
= substream
;
279 timer_setup(&info
->stream_timer
[substream
->stream
].timer
,
280 imx_rpmsg_timer_callback
, 0);
284 static int imx_rpmsg_pcm_close(struct snd_soc_component
*component
,
285 struct snd_pcm_substream
*substream
)
287 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
288 struct rpmsg_info
*info
= dev_get_drvdata(component
->dev
);
289 struct rpmsg_msg
*msg
;
291 /* Flush work in workqueue to make TX_CLOSE is the last message */
292 flush_workqueue(info
->rpmsg_wq
);
294 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
295 msg
= &info
->msg
[TX_CLOSE
];
296 msg
->s_msg
.header
.cmd
= TX_CLOSE
;
298 msg
= &info
->msg
[RX_CLOSE
];
299 msg
->s_msg
.header
.cmd
= RX_CLOSE
;
302 info
->send_message(msg
, info
);
304 del_timer(&info
->stream_timer
[substream
->stream
].timer
);
306 rtd
->dai_link
->ignore_suspend
= 0;
308 if (info
->msg_drop_count
[substream
->stream
])
309 dev_warn(rtd
->dev
, "Msg is dropped!, number is %d\n",
310 info
->msg_drop_count
[substream
->stream
]);
315 static int imx_rpmsg_pcm_prepare(struct snd_soc_component
*component
,
316 struct snd_pcm_substream
*substream
)
318 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
319 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
320 struct snd_soc_dai
*cpu_dai
= snd_soc_rtd_to_cpu(rtd
, 0);
321 struct fsl_rpmsg
*rpmsg
= dev_get_drvdata(cpu_dai
->dev
);
324 * NON-MMAP mode, NONBLOCK, Version 2, enable lpa in dts
325 * four conditions to determine the lpa is enabled.
327 if ((runtime
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
||
328 runtime
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
) &&
331 * Ignore suspend operation in low power mode
332 * M core will continue playback music on A core suspend.
334 rtd
->dai_link
->ignore_suspend
= 1;
335 rpmsg
->force_lpa
= 1;
337 rpmsg
->force_lpa
= 0;
343 static void imx_rpmsg_pcm_dma_complete(void *arg
)
345 struct snd_pcm_substream
*substream
= arg
;
347 snd_pcm_period_elapsed(substream
);
350 static int imx_rpmsg_prepare_and_submit(struct snd_soc_component
*component
,
351 struct snd_pcm_substream
*substream
)
353 struct rpmsg_info
*info
= dev_get_drvdata(component
->dev
);
354 struct rpmsg_msg
*msg
;
356 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
357 msg
= &info
->msg
[TX_BUFFER
];
358 msg
->s_msg
.header
.cmd
= TX_BUFFER
;
360 msg
= &info
->msg
[RX_BUFFER
];
361 msg
->s_msg
.header
.cmd
= RX_BUFFER
;
364 /* Send buffer address and buffer size */
365 msg
->s_msg
.param
.buffer_addr
= substream
->runtime
->dma_addr
;
366 msg
->s_msg
.param
.buffer_size
= snd_pcm_lib_buffer_bytes(substream
);
367 msg
->s_msg
.param
.period_size
= snd_pcm_lib_period_bytes(substream
);
368 msg
->s_msg
.param
.buffer_tail
= 0;
370 info
->num_period
[substream
->stream
] = msg
->s_msg
.param
.buffer_size
/
371 msg
->s_msg
.param
.period_size
;
373 info
->callback
[substream
->stream
] = imx_rpmsg_pcm_dma_complete
;
374 info
->callback_param
[substream
->stream
] = substream
;
376 return imx_rpmsg_insert_workqueue(substream
, msg
, info
);
379 static int imx_rpmsg_async_issue_pending(struct snd_soc_component
*component
,
380 struct snd_pcm_substream
*substream
)
382 struct rpmsg_info
*info
= dev_get_drvdata(component
->dev
);
383 struct rpmsg_msg
*msg
;
385 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
386 msg
= &info
->msg
[TX_START
];
387 msg
->s_msg
.header
.cmd
= TX_START
;
389 msg
= &info
->msg
[RX_START
];
390 msg
->s_msg
.header
.cmd
= RX_START
;
393 return imx_rpmsg_insert_workqueue(substream
, msg
, info
);
396 static int imx_rpmsg_restart(struct snd_soc_component
*component
,
397 struct snd_pcm_substream
*substream
)
399 struct rpmsg_info
*info
= dev_get_drvdata(component
->dev
);
400 struct rpmsg_msg
*msg
;
402 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
403 msg
= &info
->msg
[TX_RESTART
];
404 msg
->s_msg
.header
.cmd
= TX_RESTART
;
406 msg
= &info
->msg
[RX_RESTART
];
407 msg
->s_msg
.header
.cmd
= RX_RESTART
;
410 return imx_rpmsg_insert_workqueue(substream
, msg
, info
);
413 static int imx_rpmsg_pause(struct snd_soc_component
*component
,
414 struct snd_pcm_substream
*substream
)
416 struct rpmsg_info
*info
= dev_get_drvdata(component
->dev
);
417 struct rpmsg_msg
*msg
;
419 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
420 msg
= &info
->msg
[TX_PAUSE
];
421 msg
->s_msg
.header
.cmd
= TX_PAUSE
;
423 msg
= &info
->msg
[RX_PAUSE
];
424 msg
->s_msg
.header
.cmd
= RX_PAUSE
;
427 return imx_rpmsg_insert_workqueue(substream
, msg
, info
);
430 static int imx_rpmsg_terminate_all(struct snd_soc_component
*component
,
431 struct snd_pcm_substream
*substream
)
433 struct rpmsg_info
*info
= dev_get_drvdata(component
->dev
);
434 struct rpmsg_msg
*msg
;
437 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
438 msg
= &info
->msg
[TX_TERMINATE
];
439 msg
->s_msg
.header
.cmd
= TX_TERMINATE
;
440 /* Clear buffer count*/
441 cmd
= TX_PERIOD_DONE
+ MSG_TYPE_A_NUM
;
442 info
->msg
[cmd
].s_msg
.param
.buffer_tail
= 0;
443 info
->msg
[cmd
].r_msg
.param
.buffer_tail
= 0;
444 info
->msg
[TX_POINTER
].r_msg
.param
.buffer_offset
= 0;
446 msg
= &info
->msg
[RX_TERMINATE
];
447 msg
->s_msg
.header
.cmd
= RX_TERMINATE
;
448 /* Clear buffer count*/
449 cmd
= RX_PERIOD_DONE
+ MSG_TYPE_A_NUM
;
450 info
->msg
[cmd
].s_msg
.param
.buffer_tail
= 0;
451 info
->msg
[cmd
].r_msg
.param
.buffer_tail
= 0;
452 info
->msg
[RX_POINTER
].r_msg
.param
.buffer_offset
= 0;
455 del_timer(&info
->stream_timer
[substream
->stream
].timer
);
457 return imx_rpmsg_insert_workqueue(substream
, msg
, info
);
460 static int imx_rpmsg_pcm_trigger(struct snd_soc_component
*component
,
461 struct snd_pcm_substream
*substream
, int cmd
)
463 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
464 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
465 struct snd_soc_dai
*cpu_dai
= snd_soc_rtd_to_cpu(rtd
, 0);
466 struct fsl_rpmsg
*rpmsg
= dev_get_drvdata(cpu_dai
->dev
);
470 case SNDRV_PCM_TRIGGER_START
:
471 ret
= imx_rpmsg_prepare_and_submit(component
, substream
);
474 ret
= imx_rpmsg_async_issue_pending(component
, substream
);
476 case SNDRV_PCM_TRIGGER_RESUME
:
477 if (rpmsg
->force_lpa
)
480 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
481 ret
= imx_rpmsg_restart(component
, substream
);
483 case SNDRV_PCM_TRIGGER_SUSPEND
:
484 if (!rpmsg
->force_lpa
) {
485 if (runtime
->info
& SNDRV_PCM_INFO_PAUSE
)
486 ret
= imx_rpmsg_pause(component
, substream
);
488 ret
= imx_rpmsg_terminate_all(component
, substream
);
491 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
492 ret
= imx_rpmsg_pause(component
, substream
);
494 case SNDRV_PCM_TRIGGER_STOP
:
495 ret
= imx_rpmsg_terminate_all(component
, substream
);
510 * Send the period index to M core through rpmsg, but not send
511 * all the period index to M core, reduce some unnessesary msg
512 * to reduce the pressure of rpmsg bandwidth.
514 static int imx_rpmsg_pcm_ack(struct snd_soc_component
*component
,
515 struct snd_pcm_substream
*substream
)
517 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
518 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
519 struct snd_soc_dai
*cpu_dai
= snd_soc_rtd_to_cpu(rtd
, 0);
520 struct fsl_rpmsg
*rpmsg
= dev_get_drvdata(cpu_dai
->dev
);
521 struct rpmsg_info
*info
= dev_get_drvdata(component
->dev
);
522 snd_pcm_uframes_t period_size
= runtime
->period_size
;
523 snd_pcm_sframes_t avail
;
524 struct timer_list
*timer
;
525 struct rpmsg_msg
*msg
;
530 if (!rpmsg
->force_lpa
)
533 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
534 msg
= &info
->msg
[TX_PERIOD_DONE
+ MSG_TYPE_A_NUM
];
535 msg
->s_msg
.header
.cmd
= TX_PERIOD_DONE
;
537 msg
= &info
->msg
[RX_PERIOD_DONE
+ MSG_TYPE_A_NUM
];
538 msg
->s_msg
.header
.cmd
= RX_PERIOD_DONE
;
541 msg
->s_msg
.header
.type
= MSG_TYPE_C
;
543 buffer_tail
= (frames_to_bytes(runtime
, runtime
->control
->appl_ptr
) %
544 snd_pcm_lib_buffer_bytes(substream
));
545 buffer_tail
= buffer_tail
/ snd_pcm_lib_period_bytes(substream
);
547 /* There is update for period index */
548 if (buffer_tail
!= msg
->s_msg
.param
.buffer_tail
) {
549 written_num
= buffer_tail
- msg
->s_msg
.param
.buffer_tail
;
551 written_num
+= runtime
->periods
;
553 msg
->s_msg
.param
.buffer_tail
= buffer_tail
;
555 /* The notification message is updated to latest */
556 spin_lock_irqsave(&info
->lock
[substream
->stream
], flags
);
557 memcpy(&info
->notify
[substream
->stream
], msg
,
558 sizeof(struct rpmsg_s_msg
));
559 info
->notify_updated
[substream
->stream
] = true;
560 spin_unlock_irqrestore(&info
->lock
[substream
->stream
], flags
);
562 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
563 avail
= snd_pcm_playback_hw_avail(runtime
);
565 avail
= snd_pcm_capture_hw_avail(runtime
);
567 timer
= &info
->stream_timer
[substream
->stream
].timer
;
569 * If the data in the buffer is less than one period before
570 * this fill, which means the data may not enough on M
571 * core side, we need to send message immediately to let
572 * M core know the pointer is updated.
573 * if there is more than one period data in the buffer before
574 * this fill, which means the data is enough on M core side,
575 * we can delay one period (using timer) to send the message
576 * for reduce the message number in workqueue, because the
577 * pointer may be updated by ack function later, we can
578 * send latest pointer to M core side.
580 if ((avail
- written_num
* period_size
) <= period_size
) {
581 imx_rpmsg_insert_workqueue(substream
, msg
, info
);
582 } else if (rpmsg
->force_lpa
&& !timer_pending(timer
)) {
585 time_msec
= (int)(runtime
->period_size
* 1000 / runtime
->rate
);
586 mod_timer(timer
, jiffies
+ msecs_to_jiffies(time_msec
));
593 static int imx_rpmsg_pcm_new(struct snd_soc_component
*component
,
594 struct snd_soc_pcm_runtime
*rtd
)
596 struct snd_card
*card
= rtd
->card
->snd_card
;
597 struct snd_pcm
*pcm
= rtd
->pcm
;
598 struct snd_soc_dai
*cpu_dai
= snd_soc_rtd_to_cpu(rtd
, 0);
599 struct fsl_rpmsg
*rpmsg
= dev_get_drvdata(cpu_dai
->dev
);
602 ret
= dma_coerce_mask_and_coherent(card
->dev
, DMA_BIT_MASK(32));
606 return snd_pcm_set_fixed_buffer_all(pcm
, SNDRV_DMA_TYPE_DEV_WC
,
607 pcm
->card
->dev
, rpmsg
->buffer_size
);
610 static const struct snd_soc_component_driver imx_rpmsg_soc_component
= {
611 .name
= IMX_PCM_DRV_NAME
,
612 .pcm_construct
= imx_rpmsg_pcm_new
,
613 .open
= imx_rpmsg_pcm_open
,
614 .close
= imx_rpmsg_pcm_close
,
615 .hw_params
= imx_rpmsg_pcm_hw_params
,
616 .trigger
= imx_rpmsg_pcm_trigger
,
617 .pointer
= imx_rpmsg_pcm_pointer
,
618 .ack
= imx_rpmsg_pcm_ack
,
619 .prepare
= imx_rpmsg_pcm_prepare
,
622 static void imx_rpmsg_pcm_work(struct work_struct
*work
)
624 struct work_of_rpmsg
*work_of_rpmsg
;
625 bool is_notification
= false;
626 struct rpmsg_info
*info
;
627 struct rpmsg_msg msg
;
630 work_of_rpmsg
= container_of(work
, struct work_of_rpmsg
, work
);
631 info
= work_of_rpmsg
->info
;
634 * Every work in the work queue, first we check if there
635 * is update for period is filled, because there may be not
636 * enough data in M core side, need to let M core know
637 * data is updated immediately.
639 spin_lock_irqsave(&info
->lock
[TX
], flags
);
640 if (info
->notify_updated
[TX
]) {
641 memcpy(&msg
, &info
->notify
[TX
], sizeof(struct rpmsg_s_msg
));
642 info
->notify_updated
[TX
] = false;
643 spin_unlock_irqrestore(&info
->lock
[TX
], flags
);
644 info
->send_message(&msg
, info
);
646 spin_unlock_irqrestore(&info
->lock
[TX
], flags
);
649 spin_lock_irqsave(&info
->lock
[RX
], flags
);
650 if (info
->notify_updated
[RX
]) {
651 memcpy(&msg
, &info
->notify
[RX
], sizeof(struct rpmsg_s_msg
));
652 info
->notify_updated
[RX
] = false;
653 spin_unlock_irqrestore(&info
->lock
[RX
], flags
);
654 info
->send_message(&msg
, info
);
656 spin_unlock_irqrestore(&info
->lock
[RX
], flags
);
659 /* Skip the notification message for it has been processed above */
660 if (work_of_rpmsg
->msg
.s_msg
.header
.type
== MSG_TYPE_C
&&
661 (work_of_rpmsg
->msg
.s_msg
.header
.cmd
== TX_PERIOD_DONE
||
662 work_of_rpmsg
->msg
.s_msg
.header
.cmd
== RX_PERIOD_DONE
))
663 is_notification
= true;
665 if (!is_notification
)
666 info
->send_message(&work_of_rpmsg
->msg
, info
);
668 /* update read index */
669 spin_lock_irqsave(&info
->wq_lock
, flags
);
670 info
->work_read_index
++;
671 info
->work_read_index
%= WORK_MAX_NUM
;
672 spin_unlock_irqrestore(&info
->wq_lock
, flags
);
675 static int imx_rpmsg_pcm_probe(struct platform_device
*pdev
)
677 struct snd_soc_component
*component
;
678 struct rpmsg_info
*info
;
681 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
685 platform_set_drvdata(pdev
, info
);
687 info
->rpdev
= container_of(pdev
->dev
.parent
, struct rpmsg_device
, dev
);
688 info
->dev
= &pdev
->dev
;
689 /* Setup work queue */
690 info
->rpmsg_wq
= alloc_ordered_workqueue(info
->rpdev
->id
.name
,
694 if (!info
->rpmsg_wq
) {
695 dev_err(&pdev
->dev
, "workqueue create failed\n");
699 /* Write index initialize 1, make it differ with the read index */
700 info
->work_write_index
= 1;
701 info
->send_message
= imx_rpmsg_pcm_send_message
;
703 for (i
= 0; i
< WORK_MAX_NUM
; i
++) {
704 INIT_WORK(&info
->work_list
[i
].work
, imx_rpmsg_pcm_work
);
705 info
->work_list
[i
].info
= info
;
709 for (i
= 0; i
< MSG_MAX_NUM
; i
++) {
710 info
->msg
[i
].s_msg
.header
.cate
= IMX_RPMSG_AUDIO
;
711 info
->msg
[i
].s_msg
.header
.major
= IMX_RMPSG_MAJOR
;
712 info
->msg
[i
].s_msg
.header
.minor
= IMX_RMPSG_MINOR
;
713 info
->msg
[i
].s_msg
.header
.type
= MSG_TYPE_A
;
714 info
->msg
[i
].s_msg
.param
.audioindex
= 0;
717 init_completion(&info
->cmd_complete
);
718 mutex_init(&info
->msg_lock
);
719 spin_lock_init(&info
->lock
[TX
]);
720 spin_lock_init(&info
->lock
[RX
]);
721 spin_lock_init(&info
->wq_lock
);
723 ret
= devm_snd_soc_register_component(&pdev
->dev
,
724 &imx_rpmsg_soc_component
,
729 component
= snd_soc_lookup_component(&pdev
->dev
, NULL
);
735 #ifdef CONFIG_DEBUG_FS
736 component
->debugfs_prefix
= "rpmsg";
743 destroy_workqueue(info
->rpmsg_wq
);
748 static void imx_rpmsg_pcm_remove(struct platform_device
*pdev
)
750 struct rpmsg_info
*info
= platform_get_drvdata(pdev
);
753 destroy_workqueue(info
->rpmsg_wq
);
756 static int imx_rpmsg_pcm_runtime_resume(struct device
*dev
)
758 struct rpmsg_info
*info
= dev_get_drvdata(dev
);
760 cpu_latency_qos_add_request(&info
->pm_qos_req
, 0);
765 static int imx_rpmsg_pcm_runtime_suspend(struct device
*dev
)
767 struct rpmsg_info
*info
= dev_get_drvdata(dev
);
769 cpu_latency_qos_remove_request(&info
->pm_qos_req
);
774 static int imx_rpmsg_pcm_suspend(struct device
*dev
)
776 struct rpmsg_info
*info
= dev_get_drvdata(dev
);
777 struct rpmsg_msg
*rpmsg_tx
;
778 struct rpmsg_msg
*rpmsg_rx
;
780 rpmsg_tx
= &info
->msg
[TX_SUSPEND
];
781 rpmsg_rx
= &info
->msg
[RX_SUSPEND
];
783 rpmsg_tx
->s_msg
.header
.cmd
= TX_SUSPEND
;
784 info
->send_message(rpmsg_tx
, info
);
786 rpmsg_rx
->s_msg
.header
.cmd
= RX_SUSPEND
;
787 info
->send_message(rpmsg_rx
, info
);
792 static int imx_rpmsg_pcm_resume(struct device
*dev
)
794 struct rpmsg_info
*info
= dev_get_drvdata(dev
);
795 struct rpmsg_msg
*rpmsg_tx
;
796 struct rpmsg_msg
*rpmsg_rx
;
798 rpmsg_tx
= &info
->msg
[TX_RESUME
];
799 rpmsg_rx
= &info
->msg
[RX_RESUME
];
801 rpmsg_tx
->s_msg
.header
.cmd
= TX_RESUME
;
802 info
->send_message(rpmsg_tx
, info
);
804 rpmsg_rx
->s_msg
.header
.cmd
= RX_RESUME
;
805 info
->send_message(rpmsg_rx
, info
);
810 static const struct dev_pm_ops imx_rpmsg_pcm_pm_ops
= {
811 RUNTIME_PM_OPS(imx_rpmsg_pcm_runtime_suspend
,
812 imx_rpmsg_pcm_runtime_resume
, NULL
)
813 SYSTEM_SLEEP_PM_OPS(imx_rpmsg_pcm_suspend
, imx_rpmsg_pcm_resume
)
816 static const struct platform_device_id imx_rpmsg_pcm_id_table
[] = {
817 { .name
= "rpmsg-audio-channel" },
818 { .name
= "rpmsg-micfil-channel" },
821 MODULE_DEVICE_TABLE(platform
, imx_rpmsg_pcm_id_table
);
823 static struct platform_driver imx_pcm_rpmsg_driver
= {
824 .probe
= imx_rpmsg_pcm_probe
,
825 .remove
= imx_rpmsg_pcm_remove
,
826 .id_table
= imx_rpmsg_pcm_id_table
,
828 .name
= IMX_PCM_DRV_NAME
,
829 .pm
= pm_ptr(&imx_rpmsg_pcm_pm_ops
),
832 module_platform_driver(imx_pcm_rpmsg_driver
);
834 MODULE_DESCRIPTION("Freescale SoC Audio RPMSG PCM interface");
835 MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>");
836 MODULE_ALIAS("platform:" IMX_PCM_DRV_NAME
);
837 MODULE_LICENSE("GPL v2");