xref: /openbmc/qemu/hw/audio/wm8750.c (revision 1d76437b)
1 /*
2  * WM8750 audio CODEC.
3  *
4  * Copyright (c) 2006 Openedhand Ltd.
5  * Written by Andrzej Zaborowski <balrog@zabor.org>
6  *
7  * This file is licensed under GNU GPL.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "hw/i2c/i2c.h"
12 #include "migration/vmstate.h"
13 #include "qemu/module.h"
14 #include "hw/audio/wm8750.h"
15 #include "audio/audio.h"
16 #include "qom/object.h"
17 
18 #define IN_PORT_N	3
19 #define OUT_PORT_N	3
20 
21 #define CODEC		"wm8750"
22 
23 typedef struct {
24     int adc;
25     int adc_hz;
26     int dac;
27     int dac_hz;
28 } WMRate;
29 
30 OBJECT_DECLARE_SIMPLE_TYPE(WM8750State, WM8750)
31 
32 struct WM8750State {
33     I2CSlave parent_obj;
34 
35     uint8_t i2c_data[2];
36     int i2c_len;
37     QEMUSoundCard card;
38     SWVoiceIn *adc_voice[IN_PORT_N];
39     SWVoiceOut *dac_voice[OUT_PORT_N];
40     int enable;
41     void (*data_req)(void *, int, int);
42     void *opaque;
43     uint8_t data_in[4096];
44     uint8_t data_out[4096];
45     int idx_in, req_in;
46     int idx_out, req_out;
47 
48     SWVoiceOut **out[2];
49     uint8_t outvol[7], outmute[2];
50     SWVoiceIn **in[2];
51     uint8_t invol[4], inmute[2];
52 
53     uint8_t diff[2], pol, ds, monomix[2], alc, mute;
54     uint8_t path[4], mpath[2], power, format;
55     const WMRate *rate;
56     uint8_t rate_vmstate;
57     int adc_hz, dac_hz, ext_adc_hz, ext_dac_hz, master;
58 };
59 
60 /* pow(10.0, -i / 20.0) * 255, i = 0..42 */
61 static const uint8_t wm8750_vol_db_table[] = {
62     255, 227, 203, 181, 161, 143, 128, 114, 102, 90, 81, 72, 64, 57, 51, 45,
63     40, 36, 32, 29, 26, 23, 20, 18, 16, 14, 13, 11, 10, 9, 8, 7, 6, 6, 5, 5,
64     4, 4, 3, 3, 3, 2, 2
65 };
66 
67 #define WM8750_OUTVOL_TRANSFORM(x)	wm8750_vol_db_table[(0x7f - x) / 3]
68 #define WM8750_INVOL_TRANSFORM(x)	(x << 2)
69 
70 static inline void wm8750_in_load(WM8750State *s)
71 {
72     if (s->idx_in + s->req_in <= sizeof(s->data_in))
73         return;
74     s->idx_in = MAX(0, (int) sizeof(s->data_in) - s->req_in);
75     AUD_read(*s->in[0], s->data_in + s->idx_in,
76              sizeof(s->data_in) - s->idx_in);
77 }
78 
79 static inline void wm8750_out_flush(WM8750State *s)
80 {
81     int sent = 0;
82     while (sent < s->idx_out)
83         sent += AUD_write(*s->out[0], s->data_out + sent, s->idx_out - sent)
84                 ?: s->idx_out;
85     s->idx_out = 0;
86 }
87 
88 static void wm8750_audio_in_cb(void *opaque, int avail_b)
89 {
90     WM8750State *s = (WM8750State *) opaque;
91     s->req_in = avail_b;
92     s->data_req(s->opaque, s->req_out >> 2, avail_b >> 2);
93 }
94 
95 static void wm8750_audio_out_cb(void *opaque, int free_b)
96 {
97     WM8750State *s = (WM8750State *) opaque;
98 
99     if (s->idx_out >= free_b) {
100         s->idx_out = free_b;
101         s->req_out = 0;
102         wm8750_out_flush(s);
103     } else
104         s->req_out = free_b - s->idx_out;
105 
106     s->data_req(s->opaque, s->req_out >> 2, s->req_in >> 2);
107 }
108 
109 static const WMRate wm_rate_table[] = {
110     {  256, 48000,  256, 48000 },	/* SR: 00000 */
111     {  384, 48000,  384, 48000 },	/* SR: 00001 */
112     {  256, 48000, 1536,  8000 },	/* SR: 00010 */
113     {  384, 48000, 2304,  8000 },	/* SR: 00011 */
114     { 1536,  8000,  256, 48000 },	/* SR: 00100 */
115     { 2304,  8000,  384, 48000 },	/* SR: 00101 */
116     { 1536,  8000, 1536,  8000 },	/* SR: 00110 */
117     { 2304,  8000, 2304,  8000 },	/* SR: 00111 */
118     { 1024, 12000, 1024, 12000 },	/* SR: 01000 */
119     { 1526, 12000, 1536, 12000 },	/* SR: 01001 */
120     {  768, 16000,  768, 16000 },	/* SR: 01010 */
121     { 1152, 16000, 1152, 16000 },	/* SR: 01011 */
122     {  384, 32000,  384, 32000 },	/* SR: 01100 */
123     {  576, 32000,  576, 32000 },	/* SR: 01101 */
124     {  128, 96000,  128, 96000 },	/* SR: 01110 */
125     {  192, 96000,  192, 96000 },	/* SR: 01111 */
126     {  256, 44100,  256, 44100 },	/* SR: 10000 */
127     {  384, 44100,  384, 44100 },	/* SR: 10001 */
128     {  256, 44100, 1408,  8018 },	/* SR: 10010 */
129     {  384, 44100, 2112,  8018 },	/* SR: 10011 */
130     { 1408,  8018,  256, 44100 },	/* SR: 10100 */
131     { 2112,  8018,  384, 44100 },	/* SR: 10101 */
132     { 1408,  8018, 1408,  8018 },	/* SR: 10110 */
133     { 2112,  8018, 2112,  8018 },	/* SR: 10111 */
134     { 1024, 11025, 1024, 11025 },	/* SR: 11000 */
135     { 1536, 11025, 1536, 11025 },	/* SR: 11001 */
136     {  512, 22050,  512, 22050 },	/* SR: 11010 */
137     {  768, 22050,  768, 22050 },	/* SR: 11011 */
138     {  512, 24000,  512, 24000 },	/* SR: 11100 */
139     {  768, 24000,  768, 24000 },	/* SR: 11101 */
140     {  128, 88200,  128, 88200 },	/* SR: 11110 */
141     {  192, 88200,  192, 88200 },	/* SR: 11111 */
142 };
143 
144 static void wm8750_vol_update(WM8750State *s)
145 {
146     /* FIXME: multiply all volumes by s->invol[2], s->invol[3] */
147 
148     AUD_set_volume_in(s->adc_voice[0], s->mute,
149                     s->inmute[0] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[0]),
150                     s->inmute[1] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[1]));
151     AUD_set_volume_in(s->adc_voice[1], s->mute,
152                     s->inmute[0] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[0]),
153                     s->inmute[1] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[1]));
154     AUD_set_volume_in(s->adc_voice[2], s->mute,
155                     s->inmute[0] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[0]),
156                     s->inmute[1] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[1]));
157 
158     /* FIXME: multiply all volumes by s->outvol[0], s->outvol[1] */
159 
160     /* Speaker: LOUT2VOL ROUT2VOL */
161     AUD_set_volume_out(s->dac_voice[0], s->mute,
162                     s->outmute[0] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[4]),
163                     s->outmute[1] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[5]));
164 
165     /* Headphone: LOUT1VOL ROUT1VOL */
166     AUD_set_volume_out(s->dac_voice[1], s->mute,
167                     s->outmute[0] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[2]),
168                     s->outmute[1] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[3]));
169 
170     /* MONOOUT: MONOVOL MONOVOL */
171     AUD_set_volume_out(s->dac_voice[2], s->mute,
172                     s->outmute[0] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[6]),
173                     s->outmute[1] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[6]));
174 }
175 
176 static void wm8750_set_format(WM8750State *s)
177 {
178     int i;
179     struct audsettings in_fmt;
180     struct audsettings out_fmt;
181 
182     wm8750_out_flush(s);
183 
184     if (s->in[0] && *s->in[0])
185         AUD_set_active_in(*s->in[0], 0);
186     if (s->out[0] && *s->out[0])
187         AUD_set_active_out(*s->out[0], 0);
188 
189     for (i = 0; i < IN_PORT_N; i ++)
190         if (s->adc_voice[i]) {
191             AUD_close_in(&s->card, s->adc_voice[i]);
192             s->adc_voice[i] = NULL;
193         }
194     for (i = 0; i < OUT_PORT_N; i ++)
195         if (s->dac_voice[i]) {
196             AUD_close_out(&s->card, s->dac_voice[i]);
197             s->dac_voice[i] = NULL;
198         }
199 
200     if (!s->enable)
201         return;
202 
203     /* Setup input */
204     in_fmt.endianness = 0;
205     in_fmt.nchannels = 2;
206     in_fmt.freq = s->adc_hz;
207     in_fmt.fmt = AUDIO_FORMAT_S16;
208 
209     s->adc_voice[0] = AUD_open_in(&s->card, s->adc_voice[0],
210                     CODEC ".input1", s, wm8750_audio_in_cb, &in_fmt);
211     s->adc_voice[1] = AUD_open_in(&s->card, s->adc_voice[1],
212                     CODEC ".input2", s, wm8750_audio_in_cb, &in_fmt);
213     s->adc_voice[2] = AUD_open_in(&s->card, s->adc_voice[2],
214                     CODEC ".input3", s, wm8750_audio_in_cb, &in_fmt);
215 
216     /* Setup output */
217     out_fmt.endianness = 0;
218     out_fmt.nchannels = 2;
219     out_fmt.freq = s->dac_hz;
220     out_fmt.fmt = AUDIO_FORMAT_S16;
221 
222     s->dac_voice[0] = AUD_open_out(&s->card, s->dac_voice[0],
223                     CODEC ".speaker", s, wm8750_audio_out_cb, &out_fmt);
224     s->dac_voice[1] = AUD_open_out(&s->card, s->dac_voice[1],
225                     CODEC ".headphone", s, wm8750_audio_out_cb, &out_fmt);
226     /* MONOMIX is also in stereo for simplicity */
227     s->dac_voice[2] = AUD_open_out(&s->card, s->dac_voice[2],
228                     CODEC ".monomix", s, wm8750_audio_out_cb, &out_fmt);
229     /* no sense emulating OUT3 which is a mix of other outputs */
230 
231     wm8750_vol_update(s);
232 
233     /* We should connect the left and right channels to their
234      * respective inputs/outputs but we have completely no need
235      * for mixing or combining paths to different ports, so we
236      * connect both channels to where the left channel is routed.  */
237     if (s->in[0] && *s->in[0])
238         AUD_set_active_in(*s->in[0], 1);
239     if (s->out[0] && *s->out[0])
240         AUD_set_active_out(*s->out[0], 1);
241 }
242 
243 static void wm8750_clk_update(WM8750State *s, int ext)
244 {
245     if (s->master || !s->ext_dac_hz)
246         s->dac_hz = s->rate->dac_hz;
247     else
248         s->dac_hz = s->ext_dac_hz;
249 
250     if (s->master || !s->ext_adc_hz)
251         s->adc_hz = s->rate->adc_hz;
252     else
253         s->adc_hz = s->ext_adc_hz;
254 
255     if (s->master || (!s->ext_dac_hz && !s->ext_adc_hz)) {
256         if (!ext)
257             wm8750_set_format(s);
258     } else {
259         if (ext)
260             wm8750_set_format(s);
261     }
262 }
263 
264 static void wm8750_reset(I2CSlave *i2c)
265 {
266     WM8750State *s = WM8750(i2c);
267 
268     s->rate = &wm_rate_table[0];
269     s->enable = 0;
270     wm8750_clk_update(s, 1);
271     s->diff[0] = 0;
272     s->diff[1] = 0;
273     s->ds = 0;
274     s->alc = 0;
275     s->in[0] = &s->adc_voice[0];
276     s->invol[0] = 0x17;
277     s->invol[1] = 0x17;
278     s->invol[2] = 0xc3;
279     s->invol[3] = 0xc3;
280     s->out[0] = &s->dac_voice[0];
281     s->outvol[0] = 0xff;
282     s->outvol[1] = 0xff;
283     s->outvol[2] = 0x79;
284     s->outvol[3] = 0x79;
285     s->outvol[4] = 0x79;
286     s->outvol[5] = 0x79;
287     s->outvol[6] = 0x79;
288     s->inmute[0] = 0;
289     s->inmute[1] = 0;
290     s->outmute[0] = 0;
291     s->outmute[1] = 0;
292     s->mute = 1;
293     s->path[0] = 0;
294     s->path[1] = 0;
295     s->path[2] = 0;
296     s->path[3] = 0;
297     s->mpath[0] = 0;
298     s->mpath[1] = 0;
299     s->format = 0x0a;
300     s->idx_in = sizeof(s->data_in);
301     s->req_in = 0;
302     s->idx_out = 0;
303     s->req_out = 0;
304     wm8750_vol_update(s);
305     s->i2c_len = 0;
306 }
307 
308 static int wm8750_event(I2CSlave *i2c, enum i2c_event event)
309 {
310     WM8750State *s = WM8750(i2c);
311 
312     switch (event) {
313     case I2C_START_SEND:
314         s->i2c_len = 0;
315         break;
316     case I2C_FINISH:
317 #ifdef VERBOSE
318         if (s->i2c_len < 2)
319             printf("%s: message too short (%i bytes)\n",
320                             __func__, s->i2c_len);
321 #endif
322         break;
323     default:
324         break;
325     }
326 
327     return 0;
328 }
329 
330 #define WM8750_LINVOL	0x00
331 #define WM8750_RINVOL	0x01
332 #define WM8750_LOUT1V	0x02
333 #define WM8750_ROUT1V	0x03
334 #define WM8750_ADCDAC	0x05
335 #define WM8750_IFACE	0x07
336 #define WM8750_SRATE	0x08
337 #define WM8750_LDAC	0x0a
338 #define WM8750_RDAC	0x0b
339 #define WM8750_BASS	0x0c
340 #define WM8750_TREBLE	0x0d
341 #define WM8750_RESET	0x0f
342 #define WM8750_3D	0x10
343 #define WM8750_ALC1	0x11
344 #define WM8750_ALC2	0x12
345 #define WM8750_ALC3	0x13
346 #define WM8750_NGATE	0x14
347 #define WM8750_LADC	0x15
348 #define WM8750_RADC	0x16
349 #define WM8750_ADCTL1	0x17
350 #define WM8750_ADCTL2	0x18
351 #define WM8750_PWR1	0x19
352 #define WM8750_PWR2	0x1a
353 #define WM8750_ADCTL3	0x1b
354 #define WM8750_ADCIN	0x1f
355 #define WM8750_LADCIN	0x20
356 #define WM8750_RADCIN	0x21
357 #define WM8750_LOUTM1	0x22
358 #define WM8750_LOUTM2	0x23
359 #define WM8750_ROUTM1	0x24
360 #define WM8750_ROUTM2	0x25
361 #define WM8750_MOUTM1	0x26
362 #define WM8750_MOUTM2	0x27
363 #define WM8750_LOUT2V	0x28
364 #define WM8750_ROUT2V	0x29
365 #define WM8750_MOUTV	0x2a
366 
367 static int wm8750_tx(I2CSlave *i2c, uint8_t data)
368 {
369     WM8750State *s = WM8750(i2c);
370     uint8_t cmd;
371     uint16_t value;
372 
373     if (s->i2c_len >= 2) {
374 #ifdef VERBOSE
375         printf("%s: long message (%i bytes)\n", __func__, s->i2c_len);
376 #endif
377         return 1;
378     }
379     s->i2c_data[s->i2c_len ++] = data;
380     if (s->i2c_len != 2)
381         return 0;
382 
383     cmd = s->i2c_data[0] >> 1;
384     value = ((s->i2c_data[0] << 8) | s->i2c_data[1]) & 0x1ff;
385 
386     switch (cmd) {
387     case WM8750_LADCIN:	/* ADC Signal Path Control (Left) */
388         s->diff[0] = (((value >> 6) & 3) == 3);	/* LINSEL */
389         if (s->diff[0])
390             s->in[0] = &s->adc_voice[0 + s->ds * 1];
391         else
392             s->in[0] = &s->adc_voice[((value >> 6) & 3) * 1 + 0];
393         break;
394 
395     case WM8750_RADCIN:	/* ADC Signal Path Control (Right) */
396         s->diff[1] = (((value >> 6) & 3) == 3);	/* RINSEL */
397         if (s->diff[1])
398             s->in[1] = &s->adc_voice[0 + s->ds * 1];
399         else
400             s->in[1] = &s->adc_voice[((value >> 6) & 3) * 1 + 0];
401         break;
402 
403     case WM8750_ADCIN:	/* ADC Input Mode */
404         s->ds = (value >> 8) & 1;	/* DS */
405         if (s->diff[0])
406             s->in[0] = &s->adc_voice[0 + s->ds * 1];
407         if (s->diff[1])
408             s->in[1] = &s->adc_voice[0 + s->ds * 1];
409         s->monomix[0] = (value >> 6) & 3;	/* MONOMIX */
410         break;
411 
412     case WM8750_ADCTL1:	/* Additional Control (1) */
413         s->monomix[1] = (value >> 1) & 1;	/* DMONOMIX */
414         break;
415 
416     case WM8750_PWR1:	/* Power Management (1) */
417         s->enable = ((value >> 6) & 7) == 3;	/* VMIDSEL, VREF */
418         wm8750_set_format(s);
419         break;
420 
421     case WM8750_LINVOL:	/* Left Channel PGA */
422         s->invol[0] = value & 0x3f;		/* LINVOL */
423         s->inmute[0] = (value >> 7) & 1;	/* LINMUTE */
424         wm8750_vol_update(s);
425         break;
426 
427     case WM8750_RINVOL:	/* Right Channel PGA */
428         s->invol[1] = value & 0x3f;		/* RINVOL */
429         s->inmute[1] = (value >> 7) & 1;	/* RINMUTE */
430         wm8750_vol_update(s);
431         break;
432 
433     case WM8750_ADCDAC:	/* ADC and DAC Control */
434         s->pol = (value >> 5) & 3;		/* ADCPOL */
435         s->mute = (value >> 3) & 1;		/* DACMU */
436         wm8750_vol_update(s);
437         break;
438 
439     case WM8750_ADCTL3:	/* Additional Control (3) */
440         break;
441 
442     case WM8750_LADC:	/* Left ADC Digital Volume */
443         s->invol[2] = value & 0xff;		/* LADCVOL */
444         wm8750_vol_update(s);
445         break;
446 
447     case WM8750_RADC:	/* Right ADC Digital Volume */
448         s->invol[3] = value & 0xff;		/* RADCVOL */
449         wm8750_vol_update(s);
450         break;
451 
452     case WM8750_ALC1:	/* ALC Control (1) */
453         s->alc = (value >> 7) & 3;		/* ALCSEL */
454         break;
455 
456     case WM8750_NGATE:	/* Noise Gate Control */
457     case WM8750_3D:	/* 3D enhance */
458         break;
459 
460     case WM8750_LDAC:	/* Left Channel Digital Volume */
461         s->outvol[0] = value & 0xff;		/* LDACVOL */
462         wm8750_vol_update(s);
463         break;
464 
465     case WM8750_RDAC:	/* Right Channel Digital Volume */
466         s->outvol[1] = value & 0xff;		/* RDACVOL */
467         wm8750_vol_update(s);
468         break;
469 
470     case WM8750_BASS:	/* Bass Control */
471         break;
472 
473     case WM8750_LOUTM1:	/* Left Mixer Control (1) */
474         s->path[0] = (value >> 8) & 1;		/* LD2LO */
475         /* TODO: mute/unmute respective paths */
476         wm8750_vol_update(s);
477         break;
478 
479     case WM8750_LOUTM2:	/* Left Mixer Control (2) */
480         s->path[1] = (value >> 8) & 1;		/* RD2LO */
481         /* TODO: mute/unmute respective paths */
482         wm8750_vol_update(s);
483         break;
484 
485     case WM8750_ROUTM1:	/* Right Mixer Control (1) */
486         s->path[2] = (value >> 8) & 1;		/* LD2RO */
487         /* TODO: mute/unmute respective paths */
488         wm8750_vol_update(s);
489         break;
490 
491     case WM8750_ROUTM2:	/* Right Mixer Control (2) */
492         s->path[3] = (value >> 8) & 1;		/* RD2RO */
493         /* TODO: mute/unmute respective paths */
494         wm8750_vol_update(s);
495         break;
496 
497     case WM8750_MOUTM1:	/* Mono Mixer Control (1) */
498         s->mpath[0] = (value >> 8) & 1;		/* LD2MO */
499         /* TODO: mute/unmute respective paths */
500         wm8750_vol_update(s);
501         break;
502 
503     case WM8750_MOUTM2:	/* Mono Mixer Control (2) */
504         s->mpath[1] = (value >> 8) & 1;		/* RD2MO */
505         /* TODO: mute/unmute respective paths */
506         wm8750_vol_update(s);
507         break;
508 
509     case WM8750_LOUT1V:	/* LOUT1 Volume */
510         s->outvol[2] = value & 0x7f;		/* LOUT1VOL */
511         wm8750_vol_update(s);
512         break;
513 
514     case WM8750_LOUT2V:	/* LOUT2 Volume */
515         s->outvol[4] = value & 0x7f;		/* LOUT2VOL */
516         wm8750_vol_update(s);
517         break;
518 
519     case WM8750_ROUT1V:	/* ROUT1 Volume */
520         s->outvol[3] = value & 0x7f;		/* ROUT1VOL */
521         wm8750_vol_update(s);
522         break;
523 
524     case WM8750_ROUT2V:	/* ROUT2 Volume */
525         s->outvol[5] = value & 0x7f;		/* ROUT2VOL */
526         wm8750_vol_update(s);
527         break;
528 
529     case WM8750_MOUTV:	/* MONOOUT Volume */
530         s->outvol[6] = value & 0x7f;		/* MONOOUTVOL */
531         wm8750_vol_update(s);
532         break;
533 
534     case WM8750_ADCTL2:	/* Additional Control (2) */
535         break;
536 
537     case WM8750_PWR2:	/* Power Management (2) */
538         s->power = value & 0x7e;
539         /* TODO: mute/unmute respective paths */
540         wm8750_vol_update(s);
541         break;
542 
543     case WM8750_IFACE:	/* Digital Audio Interface Format */
544         s->format = value;
545         s->master = (value >> 6) & 1;			/* MS */
546         wm8750_clk_update(s, s->master);
547         break;
548 
549     case WM8750_SRATE:	/* Clocking and Sample Rate Control */
550         s->rate = &wm_rate_table[(value >> 1) & 0x1f];
551         wm8750_clk_update(s, 0);
552         break;
553 
554     case WM8750_RESET:	/* Reset */
555         wm8750_reset(I2C_SLAVE(s));
556         break;
557 
558 #ifdef VERBOSE
559     default:
560         printf("%s: unknown register %02x\n", __func__, cmd);
561 #endif
562     }
563 
564     return 0;
565 }
566 
567 static uint8_t wm8750_rx(I2CSlave *i2c)
568 {
569     return 0x00;
570 }
571 
572 static int wm8750_pre_save(void *opaque)
573 {
574     WM8750State *s = opaque;
575 
576     s->rate_vmstate = s->rate - wm_rate_table;
577 
578     return 0;
579 }
580 
581 static int wm8750_post_load(void *opaque, int version_id)
582 {
583     WM8750State *s = opaque;
584 
585     s->rate = &wm_rate_table[s->rate_vmstate & 0x1f];
586     return 0;
587 }
588 
589 static const VMStateDescription vmstate_wm8750 = {
590     .name = CODEC,
591     .version_id = 0,
592     .minimum_version_id = 0,
593     .pre_save = wm8750_pre_save,
594     .post_load = wm8750_post_load,
595     .fields = (VMStateField[]) {
596         VMSTATE_UINT8_ARRAY(i2c_data, WM8750State, 2),
597         VMSTATE_INT32(i2c_len, WM8750State),
598         VMSTATE_INT32(enable, WM8750State),
599         VMSTATE_INT32(idx_in, WM8750State),
600         VMSTATE_INT32(req_in, WM8750State),
601         VMSTATE_INT32(idx_out, WM8750State),
602         VMSTATE_INT32(req_out, WM8750State),
603         VMSTATE_UINT8_ARRAY(outvol, WM8750State, 7),
604         VMSTATE_UINT8_ARRAY(outmute, WM8750State, 2),
605         VMSTATE_UINT8_ARRAY(invol, WM8750State, 4),
606         VMSTATE_UINT8_ARRAY(inmute, WM8750State, 2),
607         VMSTATE_UINT8_ARRAY(diff, WM8750State, 2),
608         VMSTATE_UINT8(pol, WM8750State),
609         VMSTATE_UINT8(ds, WM8750State),
610         VMSTATE_UINT8_ARRAY(monomix, WM8750State, 2),
611         VMSTATE_UINT8(alc, WM8750State),
612         VMSTATE_UINT8(mute, WM8750State),
613         VMSTATE_UINT8_ARRAY(path, WM8750State, 4),
614         VMSTATE_UINT8_ARRAY(mpath, WM8750State, 2),
615         VMSTATE_UINT8(format, WM8750State),
616         VMSTATE_UINT8(power, WM8750State),
617         VMSTATE_UINT8(rate_vmstate, WM8750State),
618         VMSTATE_I2C_SLAVE(parent_obj, WM8750State),
619         VMSTATE_END_OF_LIST()
620     }
621 };
622 
623 static void wm8750_realize(DeviceState *dev, Error **errp)
624 {
625     WM8750State *s = WM8750(dev);
626 
627     AUD_register_card(CODEC, &s->card);
628     wm8750_reset(I2C_SLAVE(s));
629 }
630 
631 #if 0
632 static void wm8750_fini(I2CSlave *i2c)
633 {
634     WM8750State *s = WM8750(i2c);
635 
636     wm8750_reset(I2C_SLAVE(s));
637     AUD_remove_card(&s->card);
638     g_free(s);
639 }
640 #endif
641 
642 void wm8750_data_req_set(DeviceState *dev, data_req_cb *data_req, void *opaque)
643 {
644     WM8750State *s = WM8750(dev);
645 
646     s->data_req = data_req;
647     s->opaque = opaque;
648 }
649 
650 void wm8750_dac_dat(void *opaque, uint32_t sample)
651 {
652     WM8750State *s = (WM8750State *) opaque;
653 
654     *(uint32_t *) &s->data_out[s->idx_out] = sample;
655     s->req_out -= 4;
656     s->idx_out += 4;
657     if (s->idx_out >= sizeof(s->data_out) || s->req_out <= 0)
658         wm8750_out_flush(s);
659 }
660 
661 void *wm8750_dac_buffer(void *opaque, int samples)
662 {
663     WM8750State *s = (WM8750State *) opaque;
664     /* XXX: Should check if there are <i>samples</i> free samples available */
665     void *ret = s->data_out + s->idx_out;
666 
667     s->idx_out += samples << 2;
668     s->req_out -= samples << 2;
669     return ret;
670 }
671 
672 void wm8750_dac_commit(void *opaque)
673 {
674     WM8750State *s = (WM8750State *) opaque;
675 
676     wm8750_out_flush(s);
677 }
678 
679 uint32_t wm8750_adc_dat(void *opaque)
680 {
681     WM8750State *s = (WM8750State *) opaque;
682     uint32_t *data;
683 
684     if (s->idx_in >= sizeof(s->data_in)) {
685         wm8750_in_load(s);
686         if (s->idx_in >= sizeof(s->data_in)) {
687             return 0x80008000; /* silence in AUDIO_FORMAT_S16 sample format */
688         }
689     }
690 
691     data = (uint32_t *) &s->data_in[s->idx_in];
692     s->req_in -= 4;
693     s->idx_in += 4;
694     return *data;
695 }
696 
697 void wm8750_set_bclk_in(void *opaque, int new_hz)
698 {
699     WM8750State *s = (WM8750State *) opaque;
700 
701     s->ext_adc_hz = new_hz;
702     s->ext_dac_hz = new_hz;
703     wm8750_clk_update(s, 1);
704 }
705 
706 static Property wm8750_properties[] = {
707     DEFINE_AUDIO_PROPERTIES(WM8750State, card),
708     DEFINE_PROP_END_OF_LIST(),
709 };
710 
711 static void wm8750_class_init(ObjectClass *klass, void *data)
712 {
713     DeviceClass *dc = DEVICE_CLASS(klass);
714     I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
715 
716     dc->realize = wm8750_realize;
717     sc->event = wm8750_event;
718     sc->recv = wm8750_rx;
719     sc->send = wm8750_tx;
720     dc->vmsd = &vmstate_wm8750;
721     device_class_set_props(dc, wm8750_properties);
722 }
723 
724 static const TypeInfo wm8750_info = {
725     .name          = TYPE_WM8750,
726     .parent        = TYPE_I2C_SLAVE,
727     .instance_size = sizeof(WM8750State),
728     .class_init    = wm8750_class_init,
729 };
730 
731 static void wm8750_register_types(void)
732 {
733     type_register_static(&wm8750_info);
734 }
735 
736 type_init(wm8750_register_types)
737