1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011 Broadcom Corporation. All rights reserved. */
4 #include <linux/slab.h>
5 #include <linux/module.h>
6 #include <linux/completion.h>
8 #include "vc_vchi_audioserv_defs.h"
10 struct bcm2835_audio_instance {
12 VCHI_SERVICE_HANDLE_T vchi_handle;
13 struct completion msg_avail_comp;
14 struct mutex vchi_mutex;
15 struct bcm2835_alsa_stream *alsa_stream;
17 unsigned int max_packet;
21 static bool force_bulk;
22 module_param(force_bulk, bool, 0444);
23 MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
25 static void bcm2835_audio_lock(struct bcm2835_audio_instance *instance)
27 mutex_lock(&instance->vchi_mutex);
28 vchi_service_use(instance->vchi_handle);
31 static void bcm2835_audio_unlock(struct bcm2835_audio_instance *instance)
33 vchi_service_release(instance->vchi_handle);
34 mutex_unlock(&instance->vchi_mutex);
37 static int bcm2835_audio_send_msg_locked(struct bcm2835_audio_instance *instance,
38 struct vc_audio_msg *m, bool wait)
43 instance->result = -1;
44 init_completion(&instance->msg_avail_comp);
47 status = vchi_queue_kernel_message(instance->vchi_handle,
50 dev_err(instance->dev,
51 "vchi message queue failed: %d, msg=%d\n",
57 if (!wait_for_completion_timeout(&instance->msg_avail_comp,
58 msecs_to_jiffies(10 * 1000))) {
59 dev_err(instance->dev,
60 "vchi message timeout, msg=%d\n", m->type);
62 } else if (instance->result) {
63 dev_err(instance->dev,
64 "vchi message response error:%d, msg=%d\n",
65 instance->result, m->type);
73 static int bcm2835_audio_send_msg(struct bcm2835_audio_instance *instance,
74 struct vc_audio_msg *m, bool wait)
78 bcm2835_audio_lock(instance);
79 err = bcm2835_audio_send_msg_locked(instance, m, wait);
80 bcm2835_audio_unlock(instance);
84 static int bcm2835_audio_send_simple(struct bcm2835_audio_instance *instance,
87 struct vc_audio_msg m = { .type = type };
89 return bcm2835_audio_send_msg(instance, &m, wait);
92 static void audio_vchi_callback(void *param,
93 const VCHI_CALLBACK_REASON_T reason,
96 struct bcm2835_audio_instance *instance = param;
97 struct vc_audio_msg m;
101 if (reason != VCHI_CALLBACK_MSG_AVAILABLE)
104 status = vchi_msg_dequeue(instance->vchi_handle,
105 &m, sizeof(m), &msg_len, VCHI_FLAGS_NONE);
106 if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
107 instance->result = m.result.success;
108 complete(&instance->msg_avail_comp);
109 } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
110 if (m.complete.cookie1 != VC_AUDIO_WRITE_COOKIE1 ||
111 m.complete.cookie2 != VC_AUDIO_WRITE_COOKIE2)
112 dev_err(instance->dev, "invalid cookie\n");
114 bcm2835_playback_fifo(instance->alsa_stream,
117 dev_err(instance->dev, "unexpected callback type=%d\n", m.type);
122 vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
123 struct bcm2835_audio_instance *instance)
125 struct service_creation params = {
126 .version = VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
127 .service_id = VC_AUDIO_SERVER_NAME,
128 .callback = audio_vchi_callback,
129 .callback_param = instance,
133 /* Open the VCHI service connections */
134 status = vchi_service_open(vchi_instance, ¶ms,
135 &instance->vchi_handle);
138 dev_err(instance->dev,
139 "failed to open VCHI service connection (status=%d)\n",
144 /* Finished with the service for now */
145 vchi_service_release(instance->vchi_handle);
150 static void vc_vchi_audio_deinit(struct bcm2835_audio_instance *instance)
154 mutex_lock(&instance->vchi_mutex);
155 vchi_service_use(instance->vchi_handle);
157 /* Close all VCHI service connections */
158 status = vchi_service_close(instance->vchi_handle);
160 dev_err(instance->dev,
161 "failed to close VCHI service connection (status=%d)\n",
165 mutex_unlock(&instance->vchi_mutex);
168 int bcm2835_new_vchi_ctx(struct device *dev, struct bcm2835_vchi_ctx *vchi_ctx)
172 /* Initialize and create a VCHI connection */
173 ret = vchi_initialise(&vchi_ctx->vchi_instance);
175 dev_err(dev, "failed to initialise VCHI instance (ret=%d)\n",
180 ret = vchi_connect(vchi_ctx->vchi_instance);
182 dev_dbg(dev, "failed to connect VCHI instance (ret=%d)\n",
185 kfree(vchi_ctx->vchi_instance);
186 vchi_ctx->vchi_instance = NULL;
194 void bcm2835_free_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx)
196 /* Close the VCHI connection - it will also free vchi_instance */
197 WARN_ON(vchi_disconnect(vchi_ctx->vchi_instance));
199 vchi_ctx->vchi_instance = NULL;
202 int bcm2835_audio_open(struct bcm2835_alsa_stream *alsa_stream)
204 struct bcm2835_vchi_ctx *vchi_ctx = alsa_stream->chip->vchi_ctx;
205 struct bcm2835_audio_instance *instance;
208 /* Allocate memory for this instance */
209 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
212 mutex_init(&instance->vchi_mutex);
213 instance->dev = alsa_stream->chip->dev;
214 instance->alsa_stream = alsa_stream;
215 alsa_stream->instance = instance;
217 err = vc_vchi_audio_init(vchi_ctx->vchi_instance,
222 err = bcm2835_audio_send_simple(instance, VC_AUDIO_MSG_TYPE_OPEN,
227 bcm2835_audio_lock(instance);
228 vchi_get_peer_version(instance->vchi_handle, &instance->peer_version);
229 bcm2835_audio_unlock(instance);
230 if (instance->peer_version < 2 || force_bulk)
231 instance->max_packet = 0; /* bulk transfer */
233 instance->max_packet = 4000;
238 vc_vchi_audio_deinit(instance);
240 alsa_stream->instance = NULL;
245 int bcm2835_audio_set_ctls(struct bcm2835_alsa_stream *alsa_stream)
247 struct bcm2835_chip *chip = alsa_stream->chip;
248 struct vc_audio_msg m = {};
250 m.type = VC_AUDIO_MSG_TYPE_CONTROL;
251 m.control.dest = chip->dest;
253 m.control.volume = CHIP_MIN_VOLUME;
255 m.control.volume = alsa2chip(chip->volume);
257 return bcm2835_audio_send_msg(alsa_stream->instance, &m, true);
260 int bcm2835_audio_set_params(struct bcm2835_alsa_stream *alsa_stream,
261 unsigned int channels, unsigned int samplerate,
264 struct vc_audio_msg m = {
265 .type = VC_AUDIO_MSG_TYPE_CONFIG,
266 .config.channels = channels,
267 .config.samplerate = samplerate,
272 /* resend ctls - alsa_stream may not have been open when first send */
273 err = bcm2835_audio_set_ctls(alsa_stream);
277 return bcm2835_audio_send_msg(alsa_stream->instance, &m, true);
280 int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream)
282 return bcm2835_audio_send_simple(alsa_stream->instance,
283 VC_AUDIO_MSG_TYPE_START, false);
286 int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream)
288 return bcm2835_audio_send_simple(alsa_stream->instance,
289 VC_AUDIO_MSG_TYPE_STOP, false);
292 /* FIXME: this doesn't seem working as expected for "draining" */
293 int bcm2835_audio_drain(struct bcm2835_alsa_stream *alsa_stream)
295 struct vc_audio_msg m = {
296 .type = VC_AUDIO_MSG_TYPE_STOP,
300 return bcm2835_audio_send_msg(alsa_stream->instance, &m, false);
303 int bcm2835_audio_close(struct bcm2835_alsa_stream *alsa_stream)
305 struct bcm2835_audio_instance *instance = alsa_stream->instance;
308 err = bcm2835_audio_send_simple(alsa_stream->instance,
309 VC_AUDIO_MSG_TYPE_CLOSE, true);
311 /* Stop the audio service */
312 vc_vchi_audio_deinit(instance);
313 alsa_stream->instance = NULL;
319 int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
320 unsigned int size, void *src)
322 struct bcm2835_audio_instance *instance = alsa_stream->instance;
323 struct vc_audio_msg m = {
324 .type = VC_AUDIO_MSG_TYPE_WRITE,
326 .write.max_packet = instance->max_packet,
327 .write.cookie1 = VC_AUDIO_WRITE_COOKIE1,
328 .write.cookie2 = VC_AUDIO_WRITE_COOKIE2,
336 bcm2835_audio_lock(instance);
337 err = bcm2835_audio_send_msg_locked(instance, &m, false);
342 if (!instance->max_packet) {
343 /* Send the message to the videocore */
344 status = vchi_bulk_queue_transmit(instance->vchi_handle,
346 VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
350 int bytes = min(instance->max_packet, count);
352 status = vchi_queue_kernel_message(instance->vchi_handle,
360 dev_err(instance->dev,
361 "failed on %d bytes transfer (status=%d)\n",
367 bcm2835_audio_unlock(instance);