lm832x: QOM'ify
[qemu/cris-port.git] / hw / audio / hda-codec.c
blob986f2a9c922ca05c589e56d0f01b667d788da47d
1 /*
2 * Copyright (C) 2010 Red Hat, Inc.
4 * written by Gerd Hoffmann <kraxel@redhat.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "hw/hw.h"
21 #include "hw/pci/pci.h"
22 #include "intel-hda.h"
23 #include "intel-hda-defs.h"
24 #include "audio/audio.h"
26 /* -------------------------------------------------------------------------- */
28 typedef struct desc_param {
29 uint32_t id;
30 uint32_t val;
31 } desc_param;
33 typedef struct desc_node {
34 uint32_t nid;
35 const char *name;
36 const desc_param *params;
37 uint32_t nparams;
38 uint32_t config;
39 uint32_t pinctl;
40 uint32_t *conn;
41 uint32_t stindex;
42 } desc_node;
44 typedef struct desc_codec {
45 const char *name;
46 uint32_t iid;
47 const desc_node *nodes;
48 uint32_t nnodes;
49 } desc_codec;
51 static const desc_param* hda_codec_find_param(const desc_node *node, uint32_t id)
53 int i;
55 for (i = 0; i < node->nparams; i++) {
56 if (node->params[i].id == id) {
57 return &node->params[i];
60 return NULL;
63 static const desc_node* hda_codec_find_node(const desc_codec *codec, uint32_t nid)
65 int i;
67 for (i = 0; i < codec->nnodes; i++) {
68 if (codec->nodes[i].nid == nid) {
69 return &codec->nodes[i];
72 return NULL;
75 static void hda_codec_parse_fmt(uint32_t format, struct audsettings *as)
77 if (format & AC_FMT_TYPE_NON_PCM) {
78 return;
81 as->freq = (format & AC_FMT_BASE_44K) ? 44100 : 48000;
83 switch ((format & AC_FMT_MULT_MASK) >> AC_FMT_MULT_SHIFT) {
84 case 1: as->freq *= 2; break;
85 case 2: as->freq *= 3; break;
86 case 3: as->freq *= 4; break;
89 switch ((format & AC_FMT_DIV_MASK) >> AC_FMT_DIV_SHIFT) {
90 case 1: as->freq /= 2; break;
91 case 2: as->freq /= 3; break;
92 case 3: as->freq /= 4; break;
93 case 4: as->freq /= 5; break;
94 case 5: as->freq /= 6; break;
95 case 6: as->freq /= 7; break;
96 case 7: as->freq /= 8; break;
99 switch (format & AC_FMT_BITS_MASK) {
100 case AC_FMT_BITS_8: as->fmt = AUD_FMT_S8; break;
101 case AC_FMT_BITS_16: as->fmt = AUD_FMT_S16; break;
102 case AC_FMT_BITS_32: as->fmt = AUD_FMT_S32; break;
105 as->nchannels = ((format & AC_FMT_CHAN_MASK) >> AC_FMT_CHAN_SHIFT) + 1;
108 /* -------------------------------------------------------------------------- */
110 * HDA codec descriptions
113 /* some defines */
115 #define QEMU_HDA_ID_VENDOR 0x1af4
116 #define QEMU_HDA_PCM_FORMATS (AC_SUPPCM_BITS_16 | \
117 0x1fc /* 16 -> 96 kHz */)
118 #define QEMU_HDA_AMP_NONE (0)
119 #define QEMU_HDA_AMP_STEPS 0x4a
121 #define PARAM mixemu
122 #define HDA_MIXER
123 #include "hda-codec-common.h"
125 #define PARAM nomixemu
126 #include "hda-codec-common.h"
128 /* -------------------------------------------------------------------------- */
130 static const char *fmt2name[] = {
131 [ AUD_FMT_U8 ] = "PCM-U8",
132 [ AUD_FMT_S8 ] = "PCM-S8",
133 [ AUD_FMT_U16 ] = "PCM-U16",
134 [ AUD_FMT_S16 ] = "PCM-S16",
135 [ AUD_FMT_U32 ] = "PCM-U32",
136 [ AUD_FMT_S32 ] = "PCM-S32",
139 typedef struct HDAAudioState HDAAudioState;
140 typedef struct HDAAudioStream HDAAudioStream;
142 struct HDAAudioStream {
143 HDAAudioState *state;
144 const desc_node *node;
145 bool output, running;
146 uint32_t stream;
147 uint32_t channel;
148 uint32_t format;
149 uint32_t gain_left, gain_right;
150 bool mute_left, mute_right;
151 struct audsettings as;
152 union {
153 SWVoiceIn *in;
154 SWVoiceOut *out;
155 } voice;
156 uint8_t buf[HDA_BUFFER_SIZE];
157 uint32_t bpos;
160 struct HDAAudioState {
161 HDACodecDevice hda;
162 const char *name;
164 QEMUSoundCard card;
165 const desc_codec *desc;
166 HDAAudioStream st[4];
167 bool running_compat[16];
168 bool running_real[2 * 16];
170 /* properties */
171 uint32_t debug;
172 bool mixer;
175 static void hda_audio_input_cb(void *opaque, int avail)
177 HDAAudioStream *st = opaque;
178 int recv = 0;
179 int len;
180 bool rc;
182 while (avail - recv >= sizeof(st->buf)) {
183 if (st->bpos != sizeof(st->buf)) {
184 len = AUD_read(st->voice.in, st->buf + st->bpos,
185 sizeof(st->buf) - st->bpos);
186 st->bpos += len;
187 recv += len;
188 if (st->bpos != sizeof(st->buf)) {
189 break;
192 rc = hda_codec_xfer(&st->state->hda, st->stream, false,
193 st->buf, sizeof(st->buf));
194 if (!rc) {
195 break;
197 st->bpos = 0;
201 static void hda_audio_output_cb(void *opaque, int avail)
203 HDAAudioStream *st = opaque;
204 int sent = 0;
205 int len;
206 bool rc;
208 while (avail - sent >= sizeof(st->buf)) {
209 if (st->bpos == sizeof(st->buf)) {
210 rc = hda_codec_xfer(&st->state->hda, st->stream, true,
211 st->buf, sizeof(st->buf));
212 if (!rc) {
213 break;
215 st->bpos = 0;
217 len = AUD_write(st->voice.out, st->buf + st->bpos,
218 sizeof(st->buf) - st->bpos);
219 st->bpos += len;
220 sent += len;
221 if (st->bpos != sizeof(st->buf)) {
222 break;
227 static void hda_audio_set_running(HDAAudioStream *st, bool running)
229 if (st->node == NULL) {
230 return;
232 if (st->running == running) {
233 return;
235 st->running = running;
236 dprint(st->state, 1, "%s: %s (stream %d)\n", st->node->name,
237 st->running ? "on" : "off", st->stream);
238 if (st->output) {
239 AUD_set_active_out(st->voice.out, st->running);
240 } else {
241 AUD_set_active_in(st->voice.in, st->running);
245 static void hda_audio_set_amp(HDAAudioStream *st)
247 bool muted;
248 uint32_t left, right;
250 if (st->node == NULL) {
251 return;
254 muted = st->mute_left && st->mute_right;
255 left = st->mute_left ? 0 : st->gain_left;
256 right = st->mute_right ? 0 : st->gain_right;
258 left = left * 255 / QEMU_HDA_AMP_STEPS;
259 right = right * 255 / QEMU_HDA_AMP_STEPS;
261 if (st->output) {
262 AUD_set_volume_out(st->voice.out, muted, left, right);
263 } else {
264 AUD_set_volume_in(st->voice.in, muted, left, right);
268 static void hda_audio_setup(HDAAudioStream *st)
270 if (st->node == NULL) {
271 return;
274 dprint(st->state, 1, "%s: format: %d x %s @ %d Hz\n",
275 st->node->name, st->as.nchannels,
276 fmt2name[st->as.fmt], st->as.freq);
278 if (st->output) {
279 st->voice.out = AUD_open_out(&st->state->card, st->voice.out,
280 st->node->name, st,
281 hda_audio_output_cb, &st->as);
282 } else {
283 st->voice.in = AUD_open_in(&st->state->card, st->voice.in,
284 st->node->name, st,
285 hda_audio_input_cb, &st->as);
289 static void hda_audio_command(HDACodecDevice *hda, uint32_t nid, uint32_t data)
291 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
292 HDAAudioStream *st;
293 const desc_node *node = NULL;
294 const desc_param *param;
295 uint32_t verb, payload, response, count, shift;
297 if ((data & 0x70000) == 0x70000) {
298 /* 12/8 id/payload */
299 verb = (data >> 8) & 0xfff;
300 payload = data & 0x00ff;
301 } else {
302 /* 4/16 id/payload */
303 verb = (data >> 8) & 0xf00;
304 payload = data & 0xffff;
307 node = hda_codec_find_node(a->desc, nid);
308 if (node == NULL) {
309 goto fail;
311 dprint(a, 2, "%s: nid %d (%s), verb 0x%x, payload 0x%x\n",
312 __FUNCTION__, nid, node->name, verb, payload);
314 switch (verb) {
315 /* all nodes */
316 case AC_VERB_PARAMETERS:
317 param = hda_codec_find_param(node, payload);
318 if (param == NULL) {
319 goto fail;
321 hda_codec_response(hda, true, param->val);
322 break;
323 case AC_VERB_GET_SUBSYSTEM_ID:
324 hda_codec_response(hda, true, a->desc->iid);
325 break;
327 /* all functions */
328 case AC_VERB_GET_CONNECT_LIST:
329 param = hda_codec_find_param(node, AC_PAR_CONNLIST_LEN);
330 count = param ? param->val : 0;
331 response = 0;
332 shift = 0;
333 while (payload < count && shift < 32) {
334 response |= node->conn[payload] << shift;
335 payload++;
336 shift += 8;
338 hda_codec_response(hda, true, response);
339 break;
341 /* pin widget */
342 case AC_VERB_GET_CONFIG_DEFAULT:
343 hda_codec_response(hda, true, node->config);
344 break;
345 case AC_VERB_GET_PIN_WIDGET_CONTROL:
346 hda_codec_response(hda, true, node->pinctl);
347 break;
348 case AC_VERB_SET_PIN_WIDGET_CONTROL:
349 if (node->pinctl != payload) {
350 dprint(a, 1, "unhandled pin control bit\n");
352 hda_codec_response(hda, true, 0);
353 break;
355 /* audio in/out widget */
356 case AC_VERB_SET_CHANNEL_STREAMID:
357 st = a->st + node->stindex;
358 if (st->node == NULL) {
359 goto fail;
361 hda_audio_set_running(st, false);
362 st->stream = (payload >> 4) & 0x0f;
363 st->channel = payload & 0x0f;
364 dprint(a, 2, "%s: stream %d, channel %d\n",
365 st->node->name, st->stream, st->channel);
366 hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]);
367 hda_codec_response(hda, true, 0);
368 break;
369 case AC_VERB_GET_CONV:
370 st = a->st + node->stindex;
371 if (st->node == NULL) {
372 goto fail;
374 response = st->stream << 4 | st->channel;
375 hda_codec_response(hda, true, response);
376 break;
377 case AC_VERB_SET_STREAM_FORMAT:
378 st = a->st + node->stindex;
379 if (st->node == NULL) {
380 goto fail;
382 st->format = payload;
383 hda_codec_parse_fmt(st->format, &st->as);
384 hda_audio_setup(st);
385 hda_codec_response(hda, true, 0);
386 break;
387 case AC_VERB_GET_STREAM_FORMAT:
388 st = a->st + node->stindex;
389 if (st->node == NULL) {
390 goto fail;
392 hda_codec_response(hda, true, st->format);
393 break;
394 case AC_VERB_GET_AMP_GAIN_MUTE:
395 st = a->st + node->stindex;
396 if (st->node == NULL) {
397 goto fail;
399 if (payload & AC_AMP_GET_LEFT) {
400 response = st->gain_left | (st->mute_left ? AC_AMP_MUTE : 0);
401 } else {
402 response = st->gain_right | (st->mute_right ? AC_AMP_MUTE : 0);
404 hda_codec_response(hda, true, response);
405 break;
406 case AC_VERB_SET_AMP_GAIN_MUTE:
407 st = a->st + node->stindex;
408 if (st->node == NULL) {
409 goto fail;
411 dprint(a, 1, "amp (%s): %s%s%s%s index %d gain %3d %s\n",
412 st->node->name,
413 (payload & AC_AMP_SET_OUTPUT) ? "o" : "-",
414 (payload & AC_AMP_SET_INPUT) ? "i" : "-",
415 (payload & AC_AMP_SET_LEFT) ? "l" : "-",
416 (payload & AC_AMP_SET_RIGHT) ? "r" : "-",
417 (payload & AC_AMP_SET_INDEX) >> AC_AMP_SET_INDEX_SHIFT,
418 (payload & AC_AMP_GAIN),
419 (payload & AC_AMP_MUTE) ? "muted" : "");
420 if (payload & AC_AMP_SET_LEFT) {
421 st->gain_left = payload & AC_AMP_GAIN;
422 st->mute_left = payload & AC_AMP_MUTE;
424 if (payload & AC_AMP_SET_RIGHT) {
425 st->gain_right = payload & AC_AMP_GAIN;
426 st->mute_right = payload & AC_AMP_MUTE;
428 hda_audio_set_amp(st);
429 hda_codec_response(hda, true, 0);
430 break;
432 /* not supported */
433 case AC_VERB_SET_POWER_STATE:
434 case AC_VERB_GET_POWER_STATE:
435 case AC_VERB_GET_SDI_SELECT:
436 hda_codec_response(hda, true, 0);
437 break;
438 default:
439 goto fail;
441 return;
443 fail:
444 dprint(a, 1, "%s: not handled: nid %d (%s), verb 0x%x, payload 0x%x\n",
445 __FUNCTION__, nid, node ? node->name : "?", verb, payload);
446 hda_codec_response(hda, true, 0);
449 static void hda_audio_stream(HDACodecDevice *hda, uint32_t stnr, bool running, bool output)
451 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
452 int s;
454 a->running_compat[stnr] = running;
455 a->running_real[output * 16 + stnr] = running;
456 for (s = 0; s < ARRAY_SIZE(a->st); s++) {
457 if (a->st[s].node == NULL) {
458 continue;
460 if (a->st[s].output != output) {
461 continue;
463 if (a->st[s].stream != stnr) {
464 continue;
466 hda_audio_set_running(&a->st[s], running);
470 static int hda_audio_init(HDACodecDevice *hda, const struct desc_codec *desc)
472 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
473 HDAAudioStream *st;
474 const desc_node *node;
475 const desc_param *param;
476 uint32_t i, type;
478 a->desc = desc;
479 a->name = object_get_typename(OBJECT(a));
480 dprint(a, 1, "%s: cad %d\n", __FUNCTION__, a->hda.cad);
482 AUD_register_card("hda", &a->card);
483 for (i = 0; i < a->desc->nnodes; i++) {
484 node = a->desc->nodes + i;
485 param = hda_codec_find_param(node, AC_PAR_AUDIO_WIDGET_CAP);
486 if (NULL == param)
487 continue;
488 type = (param->val & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
489 switch (type) {
490 case AC_WID_AUD_OUT:
491 case AC_WID_AUD_IN:
492 assert(node->stindex < ARRAY_SIZE(a->st));
493 st = a->st + node->stindex;
494 st->state = a;
495 st->node = node;
496 if (type == AC_WID_AUD_OUT) {
497 /* unmute output by default */
498 st->gain_left = QEMU_HDA_AMP_STEPS;
499 st->gain_right = QEMU_HDA_AMP_STEPS;
500 st->bpos = sizeof(st->buf);
501 st->output = true;
502 } else {
503 st->output = false;
505 st->format = AC_FMT_TYPE_PCM | AC_FMT_BITS_16 |
506 (1 << AC_FMT_CHAN_SHIFT);
507 hda_codec_parse_fmt(st->format, &st->as);
508 hda_audio_setup(st);
509 break;
512 return 0;
515 static int hda_audio_exit(HDACodecDevice *hda)
517 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
518 HDAAudioStream *st;
519 int i;
521 dprint(a, 1, "%s\n", __FUNCTION__);
522 for (i = 0; i < ARRAY_SIZE(a->st); i++) {
523 st = a->st + i;
524 if (st->node == NULL) {
525 continue;
527 if (st->output) {
528 AUD_close_out(&a->card, st->voice.out);
529 } else {
530 AUD_close_in(&a->card, st->voice.in);
533 AUD_remove_card(&a->card);
534 return 0;
537 static int hda_audio_post_load(void *opaque, int version)
539 HDAAudioState *a = opaque;
540 HDAAudioStream *st;
541 int i;
543 dprint(a, 1, "%s\n", __FUNCTION__);
544 if (version == 1) {
545 /* assume running_compat[] is for output streams */
546 for (i = 0; i < ARRAY_SIZE(a->running_compat); i++)
547 a->running_real[16 + i] = a->running_compat[i];
550 for (i = 0; i < ARRAY_SIZE(a->st); i++) {
551 st = a->st + i;
552 if (st->node == NULL)
553 continue;
554 hda_codec_parse_fmt(st->format, &st->as);
555 hda_audio_setup(st);
556 hda_audio_set_amp(st);
557 hda_audio_set_running(st, a->running_real[st->output * 16 + st->stream]);
559 return 0;
562 static void hda_audio_reset(DeviceState *dev)
564 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda.qdev, dev);
565 HDAAudioStream *st;
566 int i;
568 dprint(a, 1, "%s\n", __func__);
569 for (i = 0; i < ARRAY_SIZE(a->st); i++) {
570 st = a->st + i;
571 if (st->node != NULL) {
572 hda_audio_set_running(st, false);
577 static const VMStateDescription vmstate_hda_audio_stream = {
578 .name = "hda-audio-stream",
579 .version_id = 1,
580 .fields = (VMStateField []) {
581 VMSTATE_UINT32(stream, HDAAudioStream),
582 VMSTATE_UINT32(channel, HDAAudioStream),
583 VMSTATE_UINT32(format, HDAAudioStream),
584 VMSTATE_UINT32(gain_left, HDAAudioStream),
585 VMSTATE_UINT32(gain_right, HDAAudioStream),
586 VMSTATE_BOOL(mute_left, HDAAudioStream),
587 VMSTATE_BOOL(mute_right, HDAAudioStream),
588 VMSTATE_UINT32(bpos, HDAAudioStream),
589 VMSTATE_BUFFER(buf, HDAAudioStream),
590 VMSTATE_END_OF_LIST()
594 static const VMStateDescription vmstate_hda_audio = {
595 .name = "hda-audio",
596 .version_id = 2,
597 .post_load = hda_audio_post_load,
598 .fields = (VMStateField []) {
599 VMSTATE_STRUCT_ARRAY(st, HDAAudioState, 4, 0,
600 vmstate_hda_audio_stream,
601 HDAAudioStream),
602 VMSTATE_BOOL_ARRAY(running_compat, HDAAudioState, 16),
603 VMSTATE_BOOL_ARRAY_V(running_real, HDAAudioState, 2 * 16, 2),
604 VMSTATE_END_OF_LIST()
608 static Property hda_audio_properties[] = {
609 DEFINE_PROP_UINT32("debug", HDAAudioState, debug, 0),
610 DEFINE_PROP_BOOL("mixer", HDAAudioState, mixer, true),
611 DEFINE_PROP_END_OF_LIST(),
614 static int hda_audio_init_output(HDACodecDevice *hda)
616 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
618 if (!a->mixer) {
619 return hda_audio_init(hda, &output_nomixemu);
620 } else {
621 return hda_audio_init(hda, &output_mixemu);
625 static int hda_audio_init_duplex(HDACodecDevice *hda)
627 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
629 if (!a->mixer) {
630 return hda_audio_init(hda, &duplex_nomixemu);
631 } else {
632 return hda_audio_init(hda, &duplex_mixemu);
636 static int hda_audio_init_micro(HDACodecDevice *hda)
638 HDAAudioState *a = DO_UPCAST(HDAAudioState, hda, hda);
640 if (!a->mixer) {
641 return hda_audio_init(hda, &micro_nomixemu);
642 } else {
643 return hda_audio_init(hda, &micro_mixemu);
647 static void hda_audio_output_class_init(ObjectClass *klass, void *data)
649 DeviceClass *dc = DEVICE_CLASS(klass);
650 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
652 k->init = hda_audio_init_output;
653 k->exit = hda_audio_exit;
654 k->command = hda_audio_command;
655 k->stream = hda_audio_stream;
656 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
657 dc->desc = "HDA Audio Codec, output-only (line-out)";
658 dc->reset = hda_audio_reset;
659 dc->vmsd = &vmstate_hda_audio;
660 dc->props = hda_audio_properties;
663 static const TypeInfo hda_audio_output_info = {
664 .name = "hda-output",
665 .parent = TYPE_HDA_CODEC_DEVICE,
666 .instance_size = sizeof(HDAAudioState),
667 .class_init = hda_audio_output_class_init,
670 static void hda_audio_duplex_class_init(ObjectClass *klass, void *data)
672 DeviceClass *dc = DEVICE_CLASS(klass);
673 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
675 k->init = hda_audio_init_duplex;
676 k->exit = hda_audio_exit;
677 k->command = hda_audio_command;
678 k->stream = hda_audio_stream;
679 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
680 dc->desc = "HDA Audio Codec, duplex (line-out, line-in)";
681 dc->reset = hda_audio_reset;
682 dc->vmsd = &vmstate_hda_audio;
683 dc->props = hda_audio_properties;
686 static const TypeInfo hda_audio_duplex_info = {
687 .name = "hda-duplex",
688 .parent = TYPE_HDA_CODEC_DEVICE,
689 .instance_size = sizeof(HDAAudioState),
690 .class_init = hda_audio_duplex_class_init,
693 static void hda_audio_micro_class_init(ObjectClass *klass, void *data)
695 DeviceClass *dc = DEVICE_CLASS(klass);
696 HDACodecDeviceClass *k = HDA_CODEC_DEVICE_CLASS(klass);
698 k->init = hda_audio_init_micro;
699 k->exit = hda_audio_exit;
700 k->command = hda_audio_command;
701 k->stream = hda_audio_stream;
702 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
703 dc->desc = "HDA Audio Codec, duplex (speaker, microphone)";
704 dc->reset = hda_audio_reset;
705 dc->vmsd = &vmstate_hda_audio;
706 dc->props = hda_audio_properties;
709 static const TypeInfo hda_audio_micro_info = {
710 .name = "hda-micro",
711 .parent = TYPE_HDA_CODEC_DEVICE,
712 .instance_size = sizeof(HDAAudioState),
713 .class_init = hda_audio_micro_class_init,
716 static void hda_audio_register_types(void)
718 type_register_static(&hda_audio_output_info);
719 type_register_static(&hda_audio_duplex_info);
720 type_register_static(&hda_audio_micro_info);
723 type_init(hda_audio_register_types)