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 unsigned service_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->service_handle);
31 static void bcm2835_audio_unlock(struct bcm2835_audio_instance *instance)
33 vchi_service_release(instance->service_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 = vchiq_queue_kernel_message(instance->service_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 enum vchiq_status audio_vchi_callback(enum vchiq_reason reason,
93 struct vchiq_header *header,
94 unsigned handle, void *userdata)
96 struct bcm2835_audio_instance *instance = vchiq_get_service_userdata(handle);
97 struct vc_audio_msg *m;
99 if (reason != VCHIQ_MESSAGE_AVAILABLE)
100 return VCHIQ_SUCCESS;
102 m = (void *)header->data;
103 if (m->type == VC_AUDIO_MSG_TYPE_RESULT) {
104 instance->result = m->result.success;
105 complete(&instance->msg_avail_comp);
106 } else if (m->type == VC_AUDIO_MSG_TYPE_COMPLETE) {
107 if (m->complete.cookie1 != VC_AUDIO_WRITE_COOKIE1 ||
108 m->complete.cookie2 != VC_AUDIO_WRITE_COOKIE2)
109 dev_err(instance->dev, "invalid cookie\n");
111 bcm2835_playback_fifo(instance->alsa_stream,
114 dev_err(instance->dev, "unexpected callback type=%d\n", m->type);
117 vchiq_release_message(handle, header);
118 return VCHIQ_SUCCESS;
122 vc_vchi_audio_init(struct vchiq_instance *vchiq_instance,
123 struct bcm2835_audio_instance *instance)
125 struct vchiq_service_params params = {
126 .version = VC_AUDIOSERV_VER,
127 .version_min = VC_AUDIOSERV_MIN_VER,
128 .fourcc = VCHIQ_MAKE_FOURCC('A', 'U', 'D', 'S'),
129 .callback = audio_vchi_callback,
130 .userdata = instance,
134 /* Open the VCHI service connections */
135 status = vchi_service_open(vchiq_instance, ¶ms,
136 &instance->service_handle);
139 dev_err(instance->dev,
140 "failed to open VCHI service connection (status=%d)\n",
145 /* Finished with the service for now */
146 vchi_service_release(instance->service_handle);
151 static void vc_vchi_audio_deinit(struct bcm2835_audio_instance *instance)
155 mutex_lock(&instance->vchi_mutex);
156 vchi_service_use(instance->service_handle);
158 /* Close all VCHI service connections */
159 status = vchi_service_close(instance->service_handle);
161 dev_err(instance->dev,
162 "failed to close VCHI service connection (status=%d)\n",
166 mutex_unlock(&instance->vchi_mutex);
169 int bcm2835_new_vchi_ctx(struct device *dev, struct bcm2835_vchi_ctx *vchi_ctx)
173 /* Initialize and create a VCHI connection */
174 ret = vchi_initialise(&vchi_ctx->instance);
176 dev_err(dev, "failed to initialise VCHI instance (ret=%d)\n",
181 ret = vchi_connect(vchi_ctx->instance);
183 dev_dbg(dev, "failed to connect VCHI instance (ret=%d)\n",
186 kfree(vchi_ctx->instance);
187 vchi_ctx->instance = NULL;
195 void bcm2835_free_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx)
197 /* Close the VCHI connection - it will also free vchi_ctx->instance */
198 WARN_ON(vchi_disconnect(vchi_ctx->instance));
200 vchi_ctx->instance = NULL;
203 int bcm2835_audio_open(struct bcm2835_alsa_stream *alsa_stream)
205 struct bcm2835_vchi_ctx *vchi_ctx = alsa_stream->chip->vchi_ctx;
206 struct bcm2835_audio_instance *instance;
209 /* Allocate memory for this instance */
210 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
213 mutex_init(&instance->vchi_mutex);
214 instance->dev = alsa_stream->chip->dev;
215 instance->alsa_stream = alsa_stream;
216 alsa_stream->instance = instance;
218 err = vc_vchi_audio_init(vchi_ctx->instance,
223 err = bcm2835_audio_send_simple(instance, VC_AUDIO_MSG_TYPE_OPEN,
228 bcm2835_audio_lock(instance);
229 vchi_get_peer_version(instance->service_handle,
230 &instance->peer_version);
231 bcm2835_audio_unlock(instance);
232 if (instance->peer_version < 2 || force_bulk)
233 instance->max_packet = 0; /* bulk transfer */
235 instance->max_packet = 4000;
240 vc_vchi_audio_deinit(instance);
242 alsa_stream->instance = NULL;
247 int bcm2835_audio_set_ctls(struct bcm2835_alsa_stream *alsa_stream)
249 struct bcm2835_chip *chip = alsa_stream->chip;
250 struct vc_audio_msg m = {};
252 m.type = VC_AUDIO_MSG_TYPE_CONTROL;
253 m.control.dest = chip->dest;
255 m.control.volume = CHIP_MIN_VOLUME;
257 m.control.volume = alsa2chip(chip->volume);
259 return bcm2835_audio_send_msg(alsa_stream->instance, &m, true);
262 int bcm2835_audio_set_params(struct bcm2835_alsa_stream *alsa_stream,
263 unsigned int channels, unsigned int samplerate,
266 struct vc_audio_msg m = {
267 .type = VC_AUDIO_MSG_TYPE_CONFIG,
268 .config.channels = channels,
269 .config.samplerate = samplerate,
274 /* resend ctls - alsa_stream may not have been open when first send */
275 err = bcm2835_audio_set_ctls(alsa_stream);
279 return bcm2835_audio_send_msg(alsa_stream->instance, &m, true);
282 int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream)
284 return bcm2835_audio_send_simple(alsa_stream->instance,
285 VC_AUDIO_MSG_TYPE_START, false);
288 int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream)
290 return bcm2835_audio_send_simple(alsa_stream->instance,
291 VC_AUDIO_MSG_TYPE_STOP, false);
294 /* FIXME: this doesn't seem working as expected for "draining" */
295 int bcm2835_audio_drain(struct bcm2835_alsa_stream *alsa_stream)
297 struct vc_audio_msg m = {
298 .type = VC_AUDIO_MSG_TYPE_STOP,
302 return bcm2835_audio_send_msg(alsa_stream->instance, &m, false);
305 int bcm2835_audio_close(struct bcm2835_alsa_stream *alsa_stream)
307 struct bcm2835_audio_instance *instance = alsa_stream->instance;
310 err = bcm2835_audio_send_simple(alsa_stream->instance,
311 VC_AUDIO_MSG_TYPE_CLOSE, true);
313 /* Stop the audio service */
314 vc_vchi_audio_deinit(instance);
315 alsa_stream->instance = NULL;
321 int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
322 unsigned int size, void *src)
324 struct bcm2835_audio_instance *instance = alsa_stream->instance;
325 struct vc_audio_msg m = {
326 .type = VC_AUDIO_MSG_TYPE_WRITE,
328 .write.max_packet = instance->max_packet,
329 .write.cookie1 = VC_AUDIO_WRITE_COOKIE1,
330 .write.cookie2 = VC_AUDIO_WRITE_COOKIE2,
338 bcm2835_audio_lock(instance);
339 err = bcm2835_audio_send_msg_locked(instance, &m, false);
344 if (!instance->max_packet) {
345 /* Send the message to the videocore */
346 status = vchiq_bulk_transmit(instance->service_handle, src,
348 VCHIQ_BULK_MODE_BLOCKING);
351 int bytes = min(instance->max_packet, count);
353 status = vchiq_queue_kernel_message(instance->service_handle,
361 dev_err(instance->dev,
362 "failed on %d bytes transfer (status=%d)\n",
368 bcm2835_audio_unlock(instance);