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