4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 * This file is licensed under GNU GPL.
15 #define CODEC "wm8750"
23 SWVoiceIn
*adc_voice
[IN_PORT_N
];
24 SWVoiceOut
*dac_voice
[OUT_PORT_N
];
26 void (*data_req
)(void *, int, int);
28 uint8_t data_in
[4096];
29 uint8_t data_out
[4096];
34 uint8_t outvol
[7], outmute
[2];
36 uint8_t invol
[4], inmute
[2];
38 uint8_t diff
[2], pol
, ds
, monomix
[2], alc
, mute
;
39 uint8_t path
[4], mpath
[2], power
, format
;
40 uint32_t inmask
, outmask
;
41 const struct wm_rate_s
*rate
;
44 static inline void wm8750_in_load(struct wm8750_s
*s
)
47 if (s
->idx_in
+ s
->req_in
<= sizeof(s
->data_in
))
49 s
->idx_in
= audio_MAX(0, (int) sizeof(s
->data_in
) - s
->req_in
);
50 acquired
= AUD_read(*s
->in
[0], s
->data_in
+ s
->idx_in
,
51 sizeof(s
->data_in
) - s
->idx_in
);
54 static inline void wm8750_out_flush(struct wm8750_s
*s
)
59 sent
= AUD_write(*s
->out
[0], s
->data_out
, s
->idx_out
);
63 static void wm8750_audio_in_cb(void *opaque
, int avail_b
)
65 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
67 s
->data_req(s
->opaque
, s
->req_out
>> 2, avail_b
>> 2);
74 static void wm8750_audio_out_cb(void *opaque
, int free_b
)
76 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
80 s
->data_req(s
->opaque
, free_b
>> 2, s
->req_in
>> 2);
90 static const struct wm_rate_s wm_rate_table
[] = {
91 { 256, 48000, 256, 48000 }, /* SR: 00000 */
92 { 384, 48000, 384, 48000 }, /* SR: 00001 */
93 { 256, 48000, 1536, 8000 }, /* SR: 00010 */
94 { 384, 48000, 2304, 8000 }, /* SR: 00011 */
95 { 1536, 8000, 256, 48000 }, /* SR: 00100 */
96 { 2304, 8000, 384, 48000 }, /* SR: 00101 */
97 { 1536, 8000, 1536, 8000 }, /* SR: 00110 */
98 { 2304, 8000, 2304, 8000 }, /* SR: 00111 */
99 { 1024, 12000, 1024, 12000 }, /* SR: 01000 */
100 { 1526, 12000, 1536, 12000 }, /* SR: 01001 */
101 { 768, 16000, 768, 16000 }, /* SR: 01010 */
102 { 1152, 16000, 1152, 16000 }, /* SR: 01011 */
103 { 384, 32000, 384, 32000 }, /* SR: 01100 */
104 { 576, 32000, 576, 32000 }, /* SR: 01101 */
105 { 128, 96000, 128, 96000 }, /* SR: 01110 */
106 { 192, 96000, 192, 96000 }, /* SR: 01111 */
107 { 256, 44100, 256, 44100 }, /* SR: 10000 */
108 { 384, 44100, 384, 44100 }, /* SR: 10001 */
109 { 256, 44100, 1408, 8018 }, /* SR: 10010 */
110 { 384, 44100, 2112, 8018 }, /* SR: 10011 */
111 { 1408, 8018, 256, 44100 }, /* SR: 10100 */
112 { 2112, 8018, 384, 44100 }, /* SR: 10101 */
113 { 1408, 8018, 1408, 8018 }, /* SR: 10110 */
114 { 2112, 8018, 2112, 8018 }, /* SR: 10111 */
115 { 1024, 11025, 1024, 11025 }, /* SR: 11000 */
116 { 1536, 11025, 1536, 11025 }, /* SR: 11001 */
117 { 512, 22050, 512, 22050 }, /* SR: 11010 */
118 { 768, 22050, 768, 22050 }, /* SR: 11011 */
119 { 512, 24000, 512, 24000 }, /* SR: 11100 */
120 { 768, 24000, 768, 24000 }, /* SR: 11101 */
121 { 128, 88200, 128, 88200 }, /* SR: 11110 */
122 { 192, 88200, 128, 88200 }, /* SR: 11111 */
125 void wm8750_set_format(struct wm8750_s
*s
)
128 audsettings_t in_fmt
;
129 audsettings_t out_fmt
;
130 audsettings_t monoout_fmt
;
134 if (s
->in
[0] && *s
->in
[0])
135 AUD_set_active_in(*s
->in
[0], 0);
136 if (s
->out
[0] && *s
->out
[0])
137 AUD_set_active_out(*s
->out
[0], 0);
139 for (i
= 0; i
< IN_PORT_N
; i
++)
140 if (s
->adc_voice
[i
]) {
141 AUD_close_in(&s
->card
, s
->adc_voice
[i
]);
144 for (i
= 0; i
< OUT_PORT_N
; i
++)
145 if (s
->dac_voice
[i
]) {
146 AUD_close_out(&s
->card
, s
->dac_voice
[i
]);
154 in_fmt
.endianness
= 0;
155 in_fmt
.nchannels
= 2;
156 in_fmt
.freq
= s
->rate
->adc_hz
;
157 in_fmt
.fmt
= AUD_FMT_S16
;
159 s
->adc_voice
[0] = AUD_open_in(&s
->card
, s
->adc_voice
[0],
160 CODEC
".input1", s
, wm8750_audio_in_cb
, &in_fmt
);
161 s
->adc_voice
[1] = AUD_open_in(&s
->card
, s
->adc_voice
[1],
162 CODEC
".input2", s
, wm8750_audio_in_cb
, &in_fmt
);
163 s
->adc_voice
[2] = AUD_open_in(&s
->card
, s
->adc_voice
[2],
164 CODEC
".input3", s
, wm8750_audio_in_cb
, &in_fmt
);
167 out_fmt
.endianness
= 0;
168 out_fmt
.nchannels
= 2;
169 out_fmt
.freq
= s
->rate
->dac_hz
;
170 out_fmt
.fmt
= AUD_FMT_S16
;
171 monoout_fmt
.endianness
= 0;
172 monoout_fmt
.nchannels
= 1;
173 monoout_fmt
.freq
= s
->rate
->dac_hz
;
174 monoout_fmt
.fmt
= AUD_FMT_S16
;
176 s
->dac_voice
[0] = AUD_open_out(&s
->card
, s
->dac_voice
[0],
177 CODEC
".speaker", s
, wm8750_audio_out_cb
, &out_fmt
);
178 s
->dac_voice
[1] = AUD_open_out(&s
->card
, s
->dac_voice
[1],
179 CODEC
".headphone", s
, wm8750_audio_out_cb
, &out_fmt
);
180 /* MONOMIX is also in stereo for simplicity */
181 s
->dac_voice
[2] = AUD_open_out(&s
->card
, s
->dac_voice
[2],
182 CODEC
".monomix", s
, wm8750_audio_out_cb
, &out_fmt
);
183 /* no sense emulating OUT3 which is a mix of other outputs */
185 /* We should connect the left and right channels to their
186 * respective inputs/outputs but we have completely no need
187 * for mixing or combining paths to different ports, so we
188 * connect both channels to where the left channel is routed. */
189 if (s
->in
[0] && *s
->in
[0])
190 AUD_set_active_in(*s
->in
[0], 1);
191 if (s
->out
[0] && *s
->out
[0])
192 AUD_set_active_out(*s
->out
[0], 1);
195 void inline wm8750_mask_update(struct wm8750_s
*s
)
197 #define R_ONLY 0x0000ffff
198 #define L_ONLY 0xffff0000
199 #define BOTH (R_ONLY | L_ONLY)
200 #define NONE (R_ONLY & L_ONLY)
202 (s
->inmute
[0] ? R_ONLY
: BOTH
) &
203 (s
->inmute
[1] ? L_ONLY
: BOTH
) &
204 (s
->mute
? NONE
: BOTH
);
206 (s
->outmute
[0] ? R_ONLY
: BOTH
) &
207 (s
->outmute
[1] ? L_ONLY
: BOTH
) &
208 (s
->mute
? NONE
: BOTH
);
211 void wm8750_reset(i2c_slave
*i2c
)
213 struct wm8750_s
*s
= (struct wm8750_s
*) i2c
;
215 wm8750_set_format(s
);
220 s
->in
[0] = &s
->adc_voice
[0];
225 s
->out
[0] = &s
->dac_voice
[0];
244 s
->idx_in
= sizeof(s
->data_in
);
248 wm8750_mask_update(s
);
252 static void wm8750_event(i2c_slave
*i2c
, enum i2c_event event
)
254 struct wm8750_s
*s
= (struct wm8750_s
*) i2c
;
263 printf("%s: message too short (%i bytes)\n",
264 __FUNCTION__
, s
->i2c_len
);
272 #define WM8750_LINVOL 0x00
273 #define WM8750_RINVOL 0x01
274 #define WM8750_LOUT1V 0x02
275 #define WM8750_ROUT1V 0x03
276 #define WM8750_ADCDAC 0x05
277 #define WM8750_IFACE 0x07
278 #define WM8750_SRATE 0x08
279 #define WM8750_LDAC 0x0a
280 #define WM8750_RDAC 0x0b
281 #define WM8750_BASS 0x0c
282 #define WM8750_TREBLE 0x0d
283 #define WM8750_RESET 0x0f
284 #define WM8750_3D 0x10
285 #define WM8750_ALC1 0x11
286 #define WM8750_ALC2 0x12
287 #define WM8750_ALC3 0x13
288 #define WM8750_NGATE 0x14
289 #define WM8750_LADC 0x15
290 #define WM8750_RADC 0x16
291 #define WM8750_ADCTL1 0x17
292 #define WM8750_ADCTL2 0x18
293 #define WM8750_PWR1 0x19
294 #define WM8750_PWR2 0x1a
295 #define WM8750_ADCTL3 0x1b
296 #define WM8750_ADCIN 0x1f
297 #define WM8750_LADCIN 0x20
298 #define WM8750_RADCIN 0x21
299 #define WM8750_LOUTM1 0x22
300 #define WM8750_LOUTM2 0x23
301 #define WM8750_ROUTM1 0x24
302 #define WM8750_ROUTM2 0x25
303 #define WM8750_MOUTM1 0x26
304 #define WM8750_MOUTM2 0x27
305 #define WM8750_LOUT2V 0x28
306 #define WM8750_ROUT2V 0x29
307 #define WM8750_MOUTV 0x2a
309 static int wm8750_tx(i2c_slave
*i2c
, uint8_t data
)
311 struct wm8750_s
*s
= (struct wm8750_s
*) i2c
;
315 if (s
->i2c_len
>= 2) {
316 printf("%s: long message (%i bytes)\n", __FUNCTION__
, s
->i2c_len
);
321 s
->i2c_data
[s
->i2c_len
++] = data
;
325 cmd
= s
->i2c_data
[0] >> 1;
326 value
= ((s
->i2c_data
[0] << 8) | s
->i2c_data
[1]) & 0x1ff;
329 case WM8750_LADCIN
: /* ADC Signal Path Control (Left) */
330 s
->diff
[0] = (((value
>> 6) & 3) == 3); /* LINSEL */
332 s
->in
[0] = &s
->adc_voice
[0 + s
->ds
* 1];
334 s
->in
[0] = &s
->adc_voice
[((value
>> 6) & 3) * 1 + 0];
337 case WM8750_RADCIN
: /* ADC Signal Path Control (Right) */
338 s
->diff
[1] = (((value
>> 6) & 3) == 3); /* RINSEL */
340 s
->in
[1] = &s
->adc_voice
[0 + s
->ds
* 1];
342 s
->in
[1] = &s
->adc_voice
[((value
>> 6) & 3) * 1 + 0];
345 case WM8750_ADCIN
: /* ADC Input Mode */
346 s
->ds
= (value
>> 8) & 1; /* DS */
348 s
->in
[0] = &s
->adc_voice
[0 + s
->ds
* 1];
350 s
->in
[1] = &s
->adc_voice
[0 + s
->ds
* 1];
351 s
->monomix
[0] = (value
>> 6) & 3; /* MONOMIX */
354 case WM8750_ADCTL1
: /* Additional Control (1) */
355 s
->monomix
[1] = (value
>> 1) & 1; /* DMONOMIX */
358 case WM8750_PWR1
: /* Power Management (1) */
359 s
->enable
= ((value
>> 6) & 7) == 3; /* VMIDSEL, VREF */
360 wm8750_set_format(s
);
363 case WM8750_LINVOL
: /* Left Channel PGA */
364 s
->invol
[0] = value
& 0x3f; /* LINVOL */
365 s
->inmute
[0] = (value
>> 7) & 1; /* LINMUTE */
366 wm8750_mask_update(s
);
369 case WM8750_RINVOL
: /* Right Channel PGA */
370 s
->invol
[1] = value
& 0x3f; /* RINVOL */
371 s
->inmute
[1] = (value
>> 7) & 1; /* RINMUTE */
372 wm8750_mask_update(s
);
375 case WM8750_ADCDAC
: /* ADC and DAC Control */
376 s
->pol
= (value
>> 5) & 3; /* ADCPOL */
377 s
->mute
= (value
>> 3) & 1; /* DACMU */
378 wm8750_mask_update(s
);
381 case WM8750_ADCTL3
: /* Additional Control (3) */
384 case WM8750_LADC
: /* Left ADC Digital Volume */
385 s
->invol
[2] = value
& 0xff; /* LADCVOL */
388 case WM8750_RADC
: /* Right ADC Digital Volume */
389 s
->invol
[3] = value
& 0xff; /* RADCVOL */
392 case WM8750_ALC1
: /* ALC Control (1) */
393 s
->alc
= (value
>> 7) & 3; /* ALCSEL */
396 case WM8750_NGATE
: /* Noise Gate Control */
397 case WM8750_3D
: /* 3D enhance */
400 case WM8750_LDAC
: /* Left Channel Digital Volume */
401 s
->outvol
[0] = value
& 0xff; /* LDACVOL */
404 case WM8750_RDAC
: /* Right Channel Digital Volume */
405 s
->outvol
[1] = value
& 0xff; /* RDACVOL */
408 case WM8750_BASS
: /* Bass Control */
411 case WM8750_LOUTM1
: /* Left Mixer Control (1) */
412 s
->path
[0] = (value
>> 8) & 1; /* LD2LO */
415 case WM8750_LOUTM2
: /* Left Mixer Control (2) */
416 s
->path
[1] = (value
>> 8) & 1; /* RD2LO */
419 case WM8750_ROUTM1
: /* Right Mixer Control (1) */
420 s
->path
[2] = (value
>> 8) & 1; /* LD2RO */
423 case WM8750_ROUTM2
: /* Right Mixer Control (2) */
424 s
->path
[3] = (value
>> 8) & 1; /* RD2RO */
427 case WM8750_MOUTM1
: /* Mono Mixer Control (1) */
428 s
->mpath
[0] = (value
>> 8) & 1; /* LD2MO */
431 case WM8750_MOUTM2
: /* Mono Mixer Control (2) */
432 s
->mpath
[1] = (value
>> 8) & 1; /* RD2MO */
435 case WM8750_LOUT1V
: /* LOUT1 Volume */
436 s
->outvol
[2] = value
& 0x7f; /* LOUT2VOL */
439 case WM8750_LOUT2V
: /* LOUT2 Volume */
440 s
->outvol
[4] = value
& 0x7f; /* LOUT2VOL */
443 case WM8750_ROUT1V
: /* ROUT1 Volume */
444 s
->outvol
[3] = value
& 0x7f; /* ROUT2VOL */
447 case WM8750_ROUT2V
: /* ROUT2 Volume */
448 s
->outvol
[5] = value
& 0x7f; /* ROUT2VOL */
451 case WM8750_MOUTV
: /* MONOOUT Volume */
452 s
->outvol
[6] = value
& 0x7f; /* MONOOUTVOL */
455 case WM8750_ADCTL2
: /* Additional Control (2) */
458 case WM8750_PWR2
: /* Power Management (2) */
459 s
->power
= value
& 0x7e;
462 case WM8750_IFACE
: /* Digital Audio Interface Format */
464 if (value
& 0x40) /* MS */
465 printf("%s: attempt to enable Master Mode\n", __FUNCTION__
);
468 wm8750_set_format(s
);
471 case WM8750_SRATE
: /* Clocking and Sample Rate Control */
472 s
->rate
= &wm_rate_table
[(value
>> 1) & 0x1f];
473 wm8750_set_format(s
);
476 case WM8750_RESET
: /* Reset */
477 wm8750_reset(&s
->i2c
);
482 printf("%s: unknown register %02x\n", __FUNCTION__
, cmd
);
489 static int wm8750_rx(i2c_slave
*i2c
)
494 static void wm8750_save(QEMUFile
*f
, void *opaque
)
496 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
498 qemu_put_8s(f
, &s
->i2c_data
[0]);
499 qemu_put_8s(f
, &s
->i2c_data
[1]);
500 qemu_put_be32(f
, s
->i2c_len
);
501 qemu_put_be32(f
, s
->enable
);
502 qemu_put_be32(f
, s
->idx_in
);
503 qemu_put_be32(f
, s
->req_in
);
504 qemu_put_be32(f
, s
->idx_out
);
505 qemu_put_be32(f
, s
->req_out
);
507 for (i
= 0; i
< 7; i
++)
508 qemu_put_8s(f
, &s
->outvol
[i
]);
509 for (i
= 0; i
< 2; i
++)
510 qemu_put_8s(f
, &s
->outmute
[i
]);
511 for (i
= 0; i
< 4; i
++)
512 qemu_put_8s(f
, &s
->invol
[i
]);
513 for (i
= 0; i
< 2; i
++)
514 qemu_put_8s(f
, &s
->inmute
[i
]);
516 for (i
= 0; i
< 2; i
++)
517 qemu_put_8s(f
, &s
->diff
[i
]);
518 qemu_put_8s(f
, &s
->pol
);
519 qemu_put_8s(f
, &s
->ds
);
520 for (i
= 0; i
< 2; i
++)
521 qemu_put_8s(f
, &s
->monomix
[i
]);
522 qemu_put_8s(f
, &s
->alc
);
523 qemu_put_8s(f
, &s
->mute
);
524 for (i
= 0; i
< 4; i
++)
525 qemu_put_8s(f
, &s
->path
[i
]);
526 for (i
= 0; i
< 2; i
++)
527 qemu_put_8s(f
, &s
->mpath
[i
]);
528 qemu_put_8s(f
, &s
->format
);
529 qemu_put_8s(f
, &s
->power
);
530 qemu_put_be32s(f
, &s
->inmask
);
531 qemu_put_be32s(f
, &s
->outmask
);
532 qemu_put_byte(f
, (s
->rate
- wm_rate_table
) / sizeof(*s
->rate
));
533 i2c_slave_save(f
, &s
->i2c
);
536 static int wm8750_load(QEMUFile
*f
, void *opaque
, int version_id
)
538 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
540 qemu_get_8s(f
, &s
->i2c_data
[0]);
541 qemu_get_8s(f
, &s
->i2c_data
[1]);
542 s
->i2c_len
= qemu_get_be32(f
);
543 s
->enable
= qemu_get_be32(f
);
544 s
->idx_in
= qemu_get_be32(f
);
545 s
->req_in
= qemu_get_be32(f
);
546 s
->idx_out
= qemu_get_be32(f
);
547 s
->req_out
= qemu_get_be32(f
);
549 for (i
= 0; i
< 7; i
++)
550 qemu_get_8s(f
, &s
->outvol
[i
]);
551 for (i
= 0; i
< 2; i
++)
552 qemu_get_8s(f
, &s
->outmute
[i
]);
553 for (i
= 0; i
< 4; i
++)
554 qemu_get_8s(f
, &s
->invol
[i
]);
555 for (i
= 0; i
< 2; i
++)
556 qemu_get_8s(f
, &s
->inmute
[i
]);
558 for (i
= 0; i
< 2; i
++)
559 qemu_get_8s(f
, &s
->diff
[i
]);
560 qemu_get_8s(f
, &s
->pol
);
561 qemu_get_8s(f
, &s
->ds
);
562 for (i
= 0; i
< 2; i
++)
563 qemu_get_8s(f
, &s
->monomix
[i
]);
564 qemu_get_8s(f
, &s
->alc
);
565 qemu_get_8s(f
, &s
->mute
);
566 for (i
= 0; i
< 4; i
++)
567 qemu_get_8s(f
, &s
->path
[i
]);
568 for (i
= 0; i
< 2; i
++)
569 qemu_get_8s(f
, &s
->mpath
[i
]);
570 qemu_get_8s(f
, &s
->format
);
571 qemu_get_8s(f
, &s
->power
);
572 qemu_get_be32s(f
, &s
->inmask
);
573 qemu_get_be32s(f
, &s
->outmask
);
574 s
->rate
= &wm_rate_table
[(uint8_t) qemu_get_byte(f
) & 0x1f];
575 i2c_slave_load(f
, &s
->i2c
);
579 static int wm8750_iid
= 0;
581 i2c_slave
*wm8750_init(i2c_bus
*bus
, AudioState
*audio
)
583 struct wm8750_s
*s
= (struct wm8750_s
*)
584 i2c_slave_init(bus
, 0, sizeof(struct wm8750_s
));
585 s
->i2c
.event
= wm8750_event
;
586 s
->i2c
.recv
= wm8750_rx
;
587 s
->i2c
.send
= wm8750_tx
;
589 AUD_register_card(audio
, CODEC
, &s
->card
);
590 wm8750_reset(&s
->i2c
);
592 register_savevm(CODEC
, wm8750_iid
++, 0, wm8750_save
, wm8750_load
, s
);
597 void wm8750_fini(i2c_slave
*i2c
)
599 struct wm8750_s
*s
= (struct wm8750_s
*) i2c
;
600 wm8750_reset(&s
->i2c
);
601 AUD_remove_card(&s
->card
);
605 void wm8750_data_req_set(i2c_slave
*i2c
,
606 void (*data_req
)(void *, int, int), void *opaque
)
608 struct wm8750_s
*s
= (struct wm8750_s
*) i2c
;
609 s
->data_req
= data_req
;
613 void wm8750_dac_dat(void *opaque
, uint32_t sample
)
615 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
616 uint32_t *data
= (uint32_t *) &s
->data_out
[s
->idx_out
];
617 *data
= sample
& s
->outmask
;
620 if (s
->idx_out
>= sizeof(s
->data_out
) || s
->req_out
<= 0)
624 uint32_t wm8750_adc_dat(void *opaque
)
626 struct wm8750_s
*s
= (struct wm8750_s
*) opaque
;
628 if (s
->idx_in
>= sizeof(s
->data_in
))
630 data
= (uint32_t *) &s
->data_in
[s
->idx_in
];
633 return *data
& s
->inmask
;