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