4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 * This file is licensed under GNU GPL.
12 #include "audio/audio.h"
17 #define CODEC "wm8750"
25 SWVoiceIn
*adc_voice
[IN_PORT_N
];
26 SWVoiceOut
*dac_voice
[OUT_PORT_N
];
28 void (*data_req
)(void *, int, int);
30 uint8_t data_in
[4096];
31 uint8_t data_out
[4096];
36 uint8_t outvol
[7], outmute
[2];
38 uint8_t invol
[4], inmute
[2];
40 uint8_t diff
[2], pol
, ds
, monomix
[2], alc
, mute
;
41 uint8_t path
[4], mpath
[2], power
, format
;
42 uint32_t inmask
, outmask
;
43 const struct wm_rate_s
*rate
;
46 static inline void wm8750_in_load(struct wm8750_s
*s
)
49 if (s
->idx_in
+ s
->req_in
<= sizeof(s
->data_in
))
51 s
->idx_in
= audio_MAX(0, (int) sizeof(s
->data_in
) - s
->req_in
);
52 acquired
= AUD_read(*s
->in
[0], s
->data_in
+ s
->idx_in
,
53 sizeof(s
->data_in
) - s
->idx_in
);
56 static inline void wm8750_out_flush(struct wm8750_s
*s
)
61 sent
= AUD_write(*s
->out
[0], s
->data_out
, s
->idx_out
);
65 static void wm8750_audio_in_cb(void *opaque
, int avail_b
)
67 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
69 s
->data_req(s
->opaque
, s
->req_out
>> 2, avail_b
>> 2);
76 static void wm8750_audio_out_cb(void *opaque
, int free_b
)
78 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
82 s
->data_req(s
->opaque
, free_b
>> 2, s
->req_in
>> 2);
92 static const struct wm_rate_s wm_rate_table
[] = {
93 { 256, 48000, 256, 48000 }, /* SR: 00000 */
94 { 384, 48000, 384, 48000 }, /* SR: 00001 */
95 { 256, 48000, 1536, 8000 }, /* SR: 00010 */
96 { 384, 48000, 2304, 8000 }, /* SR: 00011 */
97 { 1536, 8000, 256, 48000 }, /* SR: 00100 */
98 { 2304, 8000, 384, 48000 }, /* SR: 00101 */
99 { 1536, 8000, 1536, 8000 }, /* SR: 00110 */
100 { 2304, 8000, 2304, 8000 }, /* SR: 00111 */
101 { 1024, 12000, 1024, 12000 }, /* SR: 01000 */
102 { 1526, 12000, 1536, 12000 }, /* SR: 01001 */
103 { 768, 16000, 768, 16000 }, /* SR: 01010 */
104 { 1152, 16000, 1152, 16000 }, /* SR: 01011 */
105 { 384, 32000, 384, 32000 }, /* SR: 01100 */
106 { 576, 32000, 576, 32000 }, /* SR: 01101 */
107 { 128, 96000, 128, 96000 }, /* SR: 01110 */
108 { 192, 96000, 192, 96000 }, /* SR: 01111 */
109 { 256, 44100, 256, 44100 }, /* SR: 10000 */
110 { 384, 44100, 384, 44100 }, /* SR: 10001 */
111 { 256, 44100, 1408, 8018 }, /* SR: 10010 */
112 { 384, 44100, 2112, 8018 }, /* SR: 10011 */
113 { 1408, 8018, 256, 44100 }, /* SR: 10100 */
114 { 2112, 8018, 384, 44100 }, /* SR: 10101 */
115 { 1408, 8018, 1408, 8018 }, /* SR: 10110 */
116 { 2112, 8018, 2112, 8018 }, /* SR: 10111 */
117 { 1024, 11025, 1024, 11025 }, /* SR: 11000 */
118 { 1536, 11025, 1536, 11025 }, /* SR: 11001 */
119 { 512, 22050, 512, 22050 }, /* SR: 11010 */
120 { 768, 22050, 768, 22050 }, /* SR: 11011 */
121 { 512, 24000, 512, 24000 }, /* SR: 11100 */
122 { 768, 24000, 768, 24000 }, /* SR: 11101 */
123 { 128, 88200, 128, 88200 }, /* SR: 11110 */
124 { 192, 88200, 128, 88200 }, /* SR: 11111 */
127 static void wm8750_set_format(struct wm8750_s
*s
)
130 audsettings_t in_fmt
;
131 audsettings_t out_fmt
;
132 audsettings_t monoout_fmt
;
136 if (s
->in
[0] && *s
->in
[0])
137 AUD_set_active_in(*s
->in
[0], 0);
138 if (s
->out
[0] && *s
->out
[0])
139 AUD_set_active_out(*s
->out
[0], 0);
141 for (i
= 0; i
< IN_PORT_N
; i
++)
142 if (s
->adc_voice
[i
]) {
143 AUD_close_in(&s
->card
, s
->adc_voice
[i
]);
146 for (i
= 0; i
< OUT_PORT_N
; i
++)
147 if (s
->dac_voice
[i
]) {
148 AUD_close_out(&s
->card
, s
->dac_voice
[i
]);
156 in_fmt
.endianness
= 0;
157 in_fmt
.nchannels
= 2;
158 in_fmt
.freq
= s
->rate
->adc_hz
;
159 in_fmt
.fmt
= AUD_FMT_S16
;
161 s
->adc_voice
[0] = AUD_open_in(&s
->card
, s
->adc_voice
[0],
162 CODEC
".input1", s
, wm8750_audio_in_cb
, &in_fmt
);
163 s
->adc_voice
[1] = AUD_open_in(&s
->card
, s
->adc_voice
[1],
164 CODEC
".input2", s
, wm8750_audio_in_cb
, &in_fmt
);
165 s
->adc_voice
[2] = AUD_open_in(&s
->card
, s
->adc_voice
[2],
166 CODEC
".input3", s
, wm8750_audio_in_cb
, &in_fmt
);
169 out_fmt
.endianness
= 0;
170 out_fmt
.nchannels
= 2;
171 out_fmt
.freq
= s
->rate
->dac_hz
;
172 out_fmt
.fmt
= AUD_FMT_S16
;
173 monoout_fmt
.endianness
= 0;
174 monoout_fmt
.nchannels
= 1;
175 monoout_fmt
.freq
= s
->rate
->dac_hz
;
176 monoout_fmt
.fmt
= AUD_FMT_S16
;
178 s
->dac_voice
[0] = AUD_open_out(&s
->card
, s
->dac_voice
[0],
179 CODEC
".speaker", s
, wm8750_audio_out_cb
, &out_fmt
);
180 s
->dac_voice
[1] = AUD_open_out(&s
->card
, s
->dac_voice
[1],
181 CODEC
".headphone", s
, wm8750_audio_out_cb
, &out_fmt
);
182 /* MONOMIX is also in stereo for simplicity */
183 s
->dac_voice
[2] = AUD_open_out(&s
->card
, s
->dac_voice
[2],
184 CODEC
".monomix", s
, wm8750_audio_out_cb
, &out_fmt
);
185 /* no sense emulating OUT3 which is a mix of other outputs */
187 /* We should connect the left and right channels to their
188 * respective inputs/outputs but we have completely no need
189 * for mixing or combining paths to different ports, so we
190 * connect both channels to where the left channel is routed. */
191 if (s
->in
[0] && *s
->in
[0])
192 AUD_set_active_in(*s
->in
[0], 1);
193 if (s
->out
[0] && *s
->out
[0])
194 AUD_set_active_out(*s
->out
[0], 1);
197 static void inline wm8750_mask_update(struct wm8750_s
*s
)
199 #define R_ONLY 0x0000ffff
200 #define L_ONLY 0xffff0000
201 #define BOTH (R_ONLY | L_ONLY)
202 #define NONE (R_ONLY & L_ONLY)
204 (s
->inmute
[0] ? R_ONLY
: BOTH
) &
205 (s
->inmute
[1] ? L_ONLY
: BOTH
) &
206 (s
->mute
? NONE
: BOTH
);
208 (s
->outmute
[0] ? R_ONLY
: BOTH
) &
209 (s
->outmute
[1] ? L_ONLY
: BOTH
) &
210 (s
->mute
? NONE
: BOTH
);
213 void wm8750_reset(i2c_slave
*i2c
)
215 struct wm8750_s
*s
= (struct wm8750_s
*) i2c
;
217 wm8750_set_format(s
);
222 s
->in
[0] = &s
->adc_voice
[0];
227 s
->out
[0] = &s
->dac_voice
[0];
246 s
->idx_in
= sizeof(s
->data_in
);
250 wm8750_mask_update(s
);
254 static void wm8750_event(i2c_slave
*i2c
, enum i2c_event event
)
256 struct wm8750_s
*s
= (struct wm8750_s
*) i2c
;
265 printf("%s: message too short (%i bytes)\n",
266 __FUNCTION__
, s
->i2c_len
);
274 #define WM8750_LINVOL 0x00
275 #define WM8750_RINVOL 0x01
276 #define WM8750_LOUT1V 0x02
277 #define WM8750_ROUT1V 0x03
278 #define WM8750_ADCDAC 0x05
279 #define WM8750_IFACE 0x07
280 #define WM8750_SRATE 0x08
281 #define WM8750_LDAC 0x0a
282 #define WM8750_RDAC 0x0b
283 #define WM8750_BASS 0x0c
284 #define WM8750_TREBLE 0x0d
285 #define WM8750_RESET 0x0f
286 #define WM8750_3D 0x10
287 #define WM8750_ALC1 0x11
288 #define WM8750_ALC2 0x12
289 #define WM8750_ALC3 0x13
290 #define WM8750_NGATE 0x14
291 #define WM8750_LADC 0x15
292 #define WM8750_RADC 0x16
293 #define WM8750_ADCTL1 0x17
294 #define WM8750_ADCTL2 0x18
295 #define WM8750_PWR1 0x19
296 #define WM8750_PWR2 0x1a
297 #define WM8750_ADCTL3 0x1b
298 #define WM8750_ADCIN 0x1f
299 #define WM8750_LADCIN 0x20
300 #define WM8750_RADCIN 0x21
301 #define WM8750_LOUTM1 0x22
302 #define WM8750_LOUTM2 0x23
303 #define WM8750_ROUTM1 0x24
304 #define WM8750_ROUTM2 0x25
305 #define WM8750_MOUTM1 0x26
306 #define WM8750_MOUTM2 0x27
307 #define WM8750_LOUT2V 0x28
308 #define WM8750_ROUT2V 0x29
309 #define WM8750_MOUTV 0x2a
311 static int wm8750_tx(i2c_slave
*i2c
, uint8_t data
)
313 struct wm8750_s
*s
= (struct wm8750_s
*) i2c
;
317 if (s
->i2c_len
>= 2) {
318 printf("%s: long message (%i bytes)\n", __FUNCTION__
, s
->i2c_len
);
323 s
->i2c_data
[s
->i2c_len
++] = data
;
327 cmd
= s
->i2c_data
[0] >> 1;
328 value
= ((s
->i2c_data
[0] << 8) | s
->i2c_data
[1]) & 0x1ff;
331 case WM8750_LADCIN
: /* ADC Signal Path Control (Left) */
332 s
->diff
[0] = (((value
>> 6) & 3) == 3); /* LINSEL */
334 s
->in
[0] = &s
->adc_voice
[0 + s
->ds
* 1];
336 s
->in
[0] = &s
->adc_voice
[((value
>> 6) & 3) * 1 + 0];
339 case WM8750_RADCIN
: /* ADC Signal Path Control (Right) */
340 s
->diff
[1] = (((value
>> 6) & 3) == 3); /* RINSEL */
342 s
->in
[1] = &s
->adc_voice
[0 + s
->ds
* 1];
344 s
->in
[1] = &s
->adc_voice
[((value
>> 6) & 3) * 1 + 0];
347 case WM8750_ADCIN
: /* ADC Input Mode */
348 s
->ds
= (value
>> 8) & 1; /* DS */
350 s
->in
[0] = &s
->adc_voice
[0 + s
->ds
* 1];
352 s
->in
[1] = &s
->adc_voice
[0 + s
->ds
* 1];
353 s
->monomix
[0] = (value
>> 6) & 3; /* MONOMIX */
356 case WM8750_ADCTL1
: /* Additional Control (1) */
357 s
->monomix
[1] = (value
>> 1) & 1; /* DMONOMIX */
360 case WM8750_PWR1
: /* Power Management (1) */
361 s
->enable
= ((value
>> 6) & 7) == 3; /* VMIDSEL, VREF */
362 wm8750_set_format(s
);
365 case WM8750_LINVOL
: /* Left Channel PGA */
366 s
->invol
[0] = value
& 0x3f; /* LINVOL */
367 s
->inmute
[0] = (value
>> 7) & 1; /* LINMUTE */
368 wm8750_mask_update(s
);
371 case WM8750_RINVOL
: /* Right Channel PGA */
372 s
->invol
[1] = value
& 0x3f; /* RINVOL */
373 s
->inmute
[1] = (value
>> 7) & 1; /* RINMUTE */
374 wm8750_mask_update(s
);
377 case WM8750_ADCDAC
: /* ADC and DAC Control */
378 s
->pol
= (value
>> 5) & 3; /* ADCPOL */
379 s
->mute
= (value
>> 3) & 1; /* DACMU */
380 wm8750_mask_update(s
);
383 case WM8750_ADCTL3
: /* Additional Control (3) */
386 case WM8750_LADC
: /* Left ADC Digital Volume */
387 s
->invol
[2] = value
& 0xff; /* LADCVOL */
390 case WM8750_RADC
: /* Right ADC Digital Volume */
391 s
->invol
[3] = value
& 0xff; /* RADCVOL */
394 case WM8750_ALC1
: /* ALC Control (1) */
395 s
->alc
= (value
>> 7) & 3; /* ALCSEL */
398 case WM8750_NGATE
: /* Noise Gate Control */
399 case WM8750_3D
: /* 3D enhance */
402 case WM8750_LDAC
: /* Left Channel Digital Volume */
403 s
->outvol
[0] = value
& 0xff; /* LDACVOL */
406 case WM8750_RDAC
: /* Right Channel Digital Volume */
407 s
->outvol
[1] = value
& 0xff; /* RDACVOL */
410 case WM8750_BASS
: /* Bass Control */
413 case WM8750_LOUTM1
: /* Left Mixer Control (1) */
414 s
->path
[0] = (value
>> 8) & 1; /* LD2LO */
417 case WM8750_LOUTM2
: /* Left Mixer Control (2) */
418 s
->path
[1] = (value
>> 8) & 1; /* RD2LO */
421 case WM8750_ROUTM1
: /* Right Mixer Control (1) */
422 s
->path
[2] = (value
>> 8) & 1; /* LD2RO */
425 case WM8750_ROUTM2
: /* Right Mixer Control (2) */
426 s
->path
[3] = (value
>> 8) & 1; /* RD2RO */
429 case WM8750_MOUTM1
: /* Mono Mixer Control (1) */
430 s
->mpath
[0] = (value
>> 8) & 1; /* LD2MO */
433 case WM8750_MOUTM2
: /* Mono Mixer Control (2) */
434 s
->mpath
[1] = (value
>> 8) & 1; /* RD2MO */
437 case WM8750_LOUT1V
: /* LOUT1 Volume */
438 s
->outvol
[2] = value
& 0x7f; /* LOUT2VOL */
441 case WM8750_LOUT2V
: /* LOUT2 Volume */
442 s
->outvol
[4] = value
& 0x7f; /* LOUT2VOL */
445 case WM8750_ROUT1V
: /* ROUT1 Volume */
446 s
->outvol
[3] = value
& 0x7f; /* ROUT2VOL */
449 case WM8750_ROUT2V
: /* ROUT2 Volume */
450 s
->outvol
[5] = value
& 0x7f; /* ROUT2VOL */
453 case WM8750_MOUTV
: /* MONOOUT Volume */
454 s
->outvol
[6] = value
& 0x7f; /* MONOOUTVOL */
457 case WM8750_ADCTL2
: /* Additional Control (2) */
460 case WM8750_PWR2
: /* Power Management (2) */
461 s
->power
= value
& 0x7e;
464 case WM8750_IFACE
: /* Digital Audio Interface Format */
466 if (value
& 0x40) /* MS */
467 printf("%s: attempt to enable Master Mode\n", __FUNCTION__
);
470 wm8750_set_format(s
);
473 case WM8750_SRATE
: /* Clocking and Sample Rate Control */
474 s
->rate
= &wm_rate_table
[(value
>> 1) & 0x1f];
475 wm8750_set_format(s
);
478 case WM8750_RESET
: /* Reset */
479 wm8750_reset(&s
->i2c
);
484 printf("%s: unknown register %02x\n", __FUNCTION__
, cmd
);
491 static int wm8750_rx(i2c_slave
*i2c
)
496 static void wm8750_save(QEMUFile
*f
, void *opaque
)
498 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
500 qemu_put_8s(f
, &s
->i2c_data
[0]);
501 qemu_put_8s(f
, &s
->i2c_data
[1]);
502 qemu_put_be32(f
, s
->i2c_len
);
503 qemu_put_be32(f
, s
->enable
);
504 qemu_put_be32(f
, s
->idx_in
);
505 qemu_put_be32(f
, s
->req_in
);
506 qemu_put_be32(f
, s
->idx_out
);
507 qemu_put_be32(f
, s
->req_out
);
509 for (i
= 0; i
< 7; i
++)
510 qemu_put_8s(f
, &s
->outvol
[i
]);
511 for (i
= 0; i
< 2; i
++)
512 qemu_put_8s(f
, &s
->outmute
[i
]);
513 for (i
= 0; i
< 4; i
++)
514 qemu_put_8s(f
, &s
->invol
[i
]);
515 for (i
= 0; i
< 2; i
++)
516 qemu_put_8s(f
, &s
->inmute
[i
]);
518 for (i
= 0; i
< 2; i
++)
519 qemu_put_8s(f
, &s
->diff
[i
]);
520 qemu_put_8s(f
, &s
->pol
);
521 qemu_put_8s(f
, &s
->ds
);
522 for (i
= 0; i
< 2; i
++)
523 qemu_put_8s(f
, &s
->monomix
[i
]);
524 qemu_put_8s(f
, &s
->alc
);
525 qemu_put_8s(f
, &s
->mute
);
526 for (i
= 0; i
< 4; i
++)
527 qemu_put_8s(f
, &s
->path
[i
]);
528 for (i
= 0; i
< 2; i
++)
529 qemu_put_8s(f
, &s
->mpath
[i
]);
530 qemu_put_8s(f
, &s
->format
);
531 qemu_put_8s(f
, &s
->power
);
532 qemu_put_be32s(f
, &s
->inmask
);
533 qemu_put_be32s(f
, &s
->outmask
);
534 qemu_put_byte(f
, (s
->rate
- wm_rate_table
) / sizeof(*s
->rate
));
535 i2c_slave_save(f
, &s
->i2c
);
538 static int wm8750_load(QEMUFile
*f
, void *opaque
, int version_id
)
540 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
542 qemu_get_8s(f
, &s
->i2c_data
[0]);
543 qemu_get_8s(f
, &s
->i2c_data
[1]);
544 s
->i2c_len
= qemu_get_be32(f
);
545 s
->enable
= qemu_get_be32(f
);
546 s
->idx_in
= qemu_get_be32(f
);
547 s
->req_in
= qemu_get_be32(f
);
548 s
->idx_out
= qemu_get_be32(f
);
549 s
->req_out
= qemu_get_be32(f
);
551 for (i
= 0; i
< 7; i
++)
552 qemu_get_8s(f
, &s
->outvol
[i
]);
553 for (i
= 0; i
< 2; i
++)
554 qemu_get_8s(f
, &s
->outmute
[i
]);
555 for (i
= 0; i
< 4; i
++)
556 qemu_get_8s(f
, &s
->invol
[i
]);
557 for (i
= 0; i
< 2; i
++)
558 qemu_get_8s(f
, &s
->inmute
[i
]);
560 for (i
= 0; i
< 2; i
++)
561 qemu_get_8s(f
, &s
->diff
[i
]);
562 qemu_get_8s(f
, &s
->pol
);
563 qemu_get_8s(f
, &s
->ds
);
564 for (i
= 0; i
< 2; i
++)
565 qemu_get_8s(f
, &s
->monomix
[i
]);
566 qemu_get_8s(f
, &s
->alc
);
567 qemu_get_8s(f
, &s
->mute
);
568 for (i
= 0; i
< 4; i
++)
569 qemu_get_8s(f
, &s
->path
[i
]);
570 for (i
= 0; i
< 2; i
++)
571 qemu_get_8s(f
, &s
->mpath
[i
]);
572 qemu_get_8s(f
, &s
->format
);
573 qemu_get_8s(f
, &s
->power
);
574 qemu_get_be32s(f
, &s
->inmask
);
575 qemu_get_be32s(f
, &s
->outmask
);
576 s
->rate
= &wm_rate_table
[(uint8_t) qemu_get_byte(f
) & 0x1f];
577 i2c_slave_load(f
, &s
->i2c
);
581 static int wm8750_iid
= 0;
583 i2c_slave
*wm8750_init(i2c_bus
*bus
, AudioState
*audio
)
585 struct wm8750_s
*s
= (struct wm8750_s
*)
586 i2c_slave_init(bus
, 0, sizeof(struct wm8750_s
));
587 s
->i2c
.event
= wm8750_event
;
588 s
->i2c
.recv
= wm8750_rx
;
589 s
->i2c
.send
= wm8750_tx
;
591 AUD_register_card(audio
, CODEC
, &s
->card
);
592 wm8750_reset(&s
->i2c
);
594 register_savevm(CODEC
, wm8750_iid
++, 0, wm8750_save
, wm8750_load
, s
);
599 static void wm8750_fini(i2c_slave
*i2c
)
601 struct wm8750_s
*s
= (struct wm8750_s
*) i2c
;
602 wm8750_reset(&s
->i2c
);
603 AUD_remove_card(&s
->card
);
607 void wm8750_data_req_set(i2c_slave
*i2c
,
608 void (*data_req
)(void *, int, int), void *opaque
)
610 struct wm8750_s
*s
= (struct wm8750_s
*) i2c
;
611 s
->data_req
= data_req
;
615 void wm8750_dac_dat(void *opaque
, uint32_t sample
)
617 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
618 uint32_t *data
= (uint32_t *) &s
->data_out
[s
->idx_out
];
619 *data
= sample
& s
->outmask
;
622 if (s
->idx_out
>= sizeof(s
->data_out
) || s
->req_out
<= 0)
626 uint32_t wm8750_adc_dat(void *opaque
)
628 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
630 if (s
->idx_in
>= sizeof(s
->data_in
))
632 data
= (uint32_t *) &s
->data_in
[s
->idx_in
];
635 return *data
& s
->inmask
;