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