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