wm8750.c 21.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * WM8750 audio CODEC.
 *
 * Copyright (c) 2006 Openedhand Ltd.
 * Written by Andrzej Zaborowski <balrog@zabor.org>
 *
 * This file is licensed under GNU GPL.
 */

P
pbrook 已提交
10 11 12
#include "hw.h"
#include "i2c.h"
#include "audio/audio.h"
13 14 15 16 17 18

#define IN_PORT_N	3
#define OUT_PORT_N	3

#define CODEC		"wm8750"

P
Paul Brook 已提交
19 20 21 22 23 24 25 26
typedef struct {
    int adc;
    int adc_hz;
    int dac;
    int dac_hz;
} WMRate;

typedef struct {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    i2c_slave i2c;
    uint8_t i2c_data[2];
    int i2c_len;
    QEMUSoundCard card;
    SWVoiceIn *adc_voice[IN_PORT_N];
    SWVoiceOut *dac_voice[OUT_PORT_N];
    int enable;
    void (*data_req)(void *, int, int);
    void *opaque;
    uint8_t data_in[4096];
    uint8_t data_out[4096];
    int idx_in, req_in;
    int idx_out, req_out;

    SWVoiceOut **out[2];
    uint8_t outvol[7], outmute[2];
    SWVoiceIn **in[2];
    uint8_t invol[4], inmute[2];

    uint8_t diff[2], pol, ds, monomix[2], alc, mute;
    uint8_t path[4], mpath[2], power, format;
P
Paul Brook 已提交
48
    const WMRate *rate;
49
    int adc_hz, dac_hz, ext_adc_hz, ext_dac_hz, master;
P
Paul Brook 已提交
50
} WM8750State;
51

B
balrog 已提交
52
/* pow(10.0, -i / 20.0) * 255, i = 0..42 */
53 54 55 56 57 58
static const uint8_t wm8750_vol_db_table[] = {
    255, 227, 203, 181, 161, 143, 128, 114, 102, 90, 81, 72, 64, 57, 51, 45,
    40, 36, 32, 29, 26, 23, 20, 18, 16, 14, 13, 11, 10, 9, 8, 7, 6, 6, 5, 5,
    4, 4, 3, 3, 3, 2, 2
};

B
balrog 已提交
59 60
#define WM8750_OUTVOL_TRANSFORM(x)	wm8750_vol_db_table[(0x7f - x) / 3]
#define WM8750_INVOL_TRANSFORM(x)	(x << 2)
61

P
Paul Brook 已提交
62
static inline void wm8750_in_load(WM8750State *s)
63 64 65 66 67 68 69 70 71
{
    int acquired;
    if (s->idx_in + s->req_in <= sizeof(s->data_in))
        return;
    s->idx_in = audio_MAX(0, (int) sizeof(s->data_in) - s->req_in);
    acquired = AUD_read(*s->in[0], s->data_in + s->idx_in,
                    sizeof(s->data_in) - s->idx_in);
}

P
Paul Brook 已提交
72
static inline void wm8750_out_flush(WM8750State *s)
73
{
B
balrog 已提交
74 75 76 77
    int sent = 0;
    while (sent < s->idx_out)
        sent += AUD_write(*s->out[0], s->data_out + sent, s->idx_out - sent)
                ?: s->idx_out;
78 79 80 81 82
    s->idx_out = 0;
}

static void wm8750_audio_in_cb(void *opaque, int avail_b)
{
P
Paul Brook 已提交
83
    WM8750State *s = (WM8750State *) opaque;
84 85 86 87 88 89
    s->req_in = avail_b;
    s->data_req(s->opaque, s->req_out >> 2, avail_b >> 2);
}

static void wm8750_audio_out_cb(void *opaque, int free_b)
{
P
Paul Brook 已提交
90
    WM8750State *s = (WM8750State *) opaque;
91

B
balrog 已提交
92 93 94 95 96 97 98 99
    if (s->idx_out >= free_b) {
        s->idx_out = free_b;
        s->req_out = 0;
        wm8750_out_flush(s);
    } else
        s->req_out = free_b - s->idx_out;
 
    s->data_req(s->opaque, s->req_out >> 2, s->req_in >> 2);
100 101
}

P
Paul Brook 已提交
102
static const WMRate wm_rate_table[] = {
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    {  256, 48000,  256, 48000 },	/* SR: 00000 */
    {  384, 48000,  384, 48000 },	/* SR: 00001 */
    {  256, 48000, 1536,  8000 },	/* SR: 00010 */
    {  384, 48000, 2304,  8000 },	/* SR: 00011 */
    { 1536,  8000,  256, 48000 },	/* SR: 00100 */
    { 2304,  8000,  384, 48000 },	/* SR: 00101 */
    { 1536,  8000, 1536,  8000 },	/* SR: 00110 */
    { 2304,  8000, 2304,  8000 },	/* SR: 00111 */
    { 1024, 12000, 1024, 12000 },	/* SR: 01000 */
    { 1526, 12000, 1536, 12000 },	/* SR: 01001 */
    {  768, 16000,  768, 16000 },	/* SR: 01010 */
    { 1152, 16000, 1152, 16000 },	/* SR: 01011 */
    {  384, 32000,  384, 32000 },	/* SR: 01100 */
    {  576, 32000,  576, 32000 },	/* SR: 01101 */
    {  128, 96000,  128, 96000 },	/* SR: 01110 */
    {  192, 96000,  192, 96000 },	/* SR: 01111 */
    {  256, 44100,  256, 44100 },	/* SR: 10000 */
    {  384, 44100,  384, 44100 },	/* SR: 10001 */
    {  256, 44100, 1408,  8018 },	/* SR: 10010 */
    {  384, 44100, 2112,  8018 },	/* SR: 10011 */
    { 1408,  8018,  256, 44100 },	/* SR: 10100 */
    { 2112,  8018,  384, 44100 },	/* SR: 10101 */
    { 1408,  8018, 1408,  8018 },	/* SR: 10110 */
    { 2112,  8018, 2112,  8018 },	/* SR: 10111 */
    { 1024, 11025, 1024, 11025 },	/* SR: 11000 */
    { 1536, 11025, 1536, 11025 },	/* SR: 11001 */
    {  512, 22050,  512, 22050 },	/* SR: 11010 */
    {  768, 22050,  768, 22050 },	/* SR: 11011 */
    {  512, 24000,  512, 24000 },	/* SR: 11100 */
    {  768, 24000,  768, 24000 },	/* SR: 11101 */
    {  128, 88200,  128, 88200 },	/* SR: 11110 */
B
balrog 已提交
134
    {  192, 88200,  192, 88200 },	/* SR: 11111 */
135 136
};

P
Paul Brook 已提交
137
static void wm8750_vol_update(WM8750State *s)
138 139 140
{
    /* FIXME: multiply all volumes by s->invol[2], s->invol[3] */

B
balrog 已提交
141 142 143 144 145 146 147 148 149
    AUD_set_volume_in(s->adc_voice[0], s->mute,
                    s->inmute[0] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[0]),
                    s->inmute[1] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[1]));
    AUD_set_volume_in(s->adc_voice[1], s->mute,
                    s->inmute[0] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[0]),
                    s->inmute[1] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[1]));
    AUD_set_volume_in(s->adc_voice[2], s->mute,
                    s->inmute[0] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[0]),
                    s->inmute[1] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[1]));
150 151 152 153 154

    /* FIXME: multiply all volumes by s->outvol[0], s->outvol[1] */

    /* Speaker: LOUT2VOL ROUT2VOL */
    AUD_set_volume_out(s->dac_voice[0], s->mute,
B
balrog 已提交
155 156
                    s->outmute[0] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[4]),
                    s->outmute[1] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[5]));
157

B
balrog 已提交
158
    /* Headphone: LOUT1VOL ROUT1VOL */
159
    AUD_set_volume_out(s->dac_voice[1], s->mute,
B
balrog 已提交
160 161
                    s->outmute[0] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[2]),
                    s->outmute[1] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[3]));
162 163 164

    /* MONOOUT: MONOVOL MONOVOL */
    AUD_set_volume_out(s->dac_voice[2], s->mute,
B
balrog 已提交
165 166
                    s->outmute[0] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[6]),
                    s->outmute[1] ? 0 : WM8750_OUTVOL_TRANSFORM(s->outvol[6]));
167 168
}

P
Paul Brook 已提交
169
static void wm8750_set_format(WM8750State *s)
170 171
{
    int i;
M
malc 已提交
172 173 174
    struct audsettings in_fmt;
    struct audsettings out_fmt;
    struct audsettings monoout_fmt;
175 176 177 178 179 180 181 182 183 184 185

    wm8750_out_flush(s);

    if (s->in[0] && *s->in[0])
        AUD_set_active_in(*s->in[0], 0);
    if (s->out[0] && *s->out[0])
        AUD_set_active_out(*s->out[0], 0);

    for (i = 0; i < IN_PORT_N; i ++)
        if (s->adc_voice[i]) {
            AUD_close_in(&s->card, s->adc_voice[i]);
186
            s->adc_voice[i] = NULL;
187 188 189 190
        }
    for (i = 0; i < OUT_PORT_N; i ++)
        if (s->dac_voice[i]) {
            AUD_close_out(&s->card, s->dac_voice[i]);
191
            s->dac_voice[i] = NULL;
192 193 194 195 196 197 198 199
        }

    if (!s->enable)
        return;

    /* Setup input */
    in_fmt.endianness = 0;
    in_fmt.nchannels = 2;
200
    in_fmt.freq = s->adc_hz;
201 202 203 204 205 206 207 208 209 210 211 212
    in_fmt.fmt = AUD_FMT_S16;

    s->adc_voice[0] = AUD_open_in(&s->card, s->adc_voice[0],
                    CODEC ".input1", s, wm8750_audio_in_cb, &in_fmt);
    s->adc_voice[1] = AUD_open_in(&s->card, s->adc_voice[1],
                    CODEC ".input2", s, wm8750_audio_in_cb, &in_fmt);
    s->adc_voice[2] = AUD_open_in(&s->card, s->adc_voice[2],
                    CODEC ".input3", s, wm8750_audio_in_cb, &in_fmt);

    /* Setup output */
    out_fmt.endianness = 0;
    out_fmt.nchannels = 2;
213
    out_fmt.freq = s->dac_hz;
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    out_fmt.fmt = AUD_FMT_S16;
    monoout_fmt.endianness = 0;
    monoout_fmt.nchannels = 1;
    monoout_fmt.freq = s->rate->dac_hz;
    monoout_fmt.fmt = AUD_FMT_S16;

    s->dac_voice[0] = AUD_open_out(&s->card, s->dac_voice[0],
                    CODEC ".speaker", s, wm8750_audio_out_cb, &out_fmt);
    s->dac_voice[1] = AUD_open_out(&s->card, s->dac_voice[1],
                    CODEC ".headphone", s, wm8750_audio_out_cb, &out_fmt);
    /* MONOMIX is also in stereo for simplicity */
    s->dac_voice[2] = AUD_open_out(&s->card, s->dac_voice[2],
                    CODEC ".monomix", s, wm8750_audio_out_cb, &out_fmt);
    /* no sense emulating OUT3 which is a mix of other outputs */

229 230
    wm8750_vol_update(s);

231 232 233 234 235 236 237 238 239 240
    /* We should connect the left and right channels to their
     * respective inputs/outputs but we have completely no need
     * for mixing or combining paths to different ports, so we
     * connect both channels to where the left channel is routed.  */
    if (s->in[0] && *s->in[0])
        AUD_set_active_in(*s->in[0], 1);
    if (s->out[0] && *s->out[0])
        AUD_set_active_out(*s->out[0], 1);
}

P
Paul Brook 已提交
241
static void wm8750_clk_update(WM8750State *s, int ext)
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
{
    if (s->master || !s->ext_dac_hz)
        s->dac_hz = s->rate->dac_hz;
    else
        s->dac_hz = s->ext_dac_hz;

    if (s->master || !s->ext_adc_hz)
        s->adc_hz = s->rate->adc_hz;
    else
        s->adc_hz = s->ext_adc_hz;

    if (s->master || (!s->ext_dac_hz && !s->ext_adc_hz)) {
        if (!ext)
            wm8750_set_format(s);
    } else {
        if (ext)
            wm8750_set_format(s);
    }
}

P
Paul Brook 已提交
262
static void wm8750_reset(i2c_slave *i2c)
263
{
P
Paul Brook 已提交
264
    WM8750State *s = (WM8750State *) i2c;
265
    s->rate = &wm_rate_table[0];
266
    s->enable = 0;
267
    wm8750_clk_update(s, 1);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    s->diff[0] = 0;
    s->diff[1] = 0;
    s->ds = 0;
    s->alc = 0;
    s->in[0] = &s->adc_voice[0];
    s->invol[0] = 0x17;
    s->invol[1] = 0x17;
    s->invol[2] = 0xc3;
    s->invol[3] = 0xc3;
    s->out[0] = &s->dac_voice[0];
    s->outvol[0] = 0xff;
    s->outvol[1] = 0xff;
    s->outvol[2] = 0x79;
    s->outvol[3] = 0x79;
    s->outvol[4] = 0x79;
    s->outvol[5] = 0x79;
B
balrog 已提交
284
    s->outvol[6] = 0x79;
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    s->inmute[0] = 0;
    s->inmute[1] = 0;
    s->outmute[0] = 0;
    s->outmute[1] = 0;
    s->mute = 1;
    s->path[0] = 0;
    s->path[1] = 0;
    s->path[2] = 0;
    s->path[3] = 0;
    s->mpath[0] = 0;
    s->mpath[1] = 0;
    s->format = 0x0a;
    s->idx_in = sizeof(s->data_in);
    s->req_in = 0;
    s->idx_out = 0;
    s->req_out = 0;
301
    wm8750_vol_update(s);
302 303 304 305 306
    s->i2c_len = 0;
}

static void wm8750_event(i2c_slave *i2c, enum i2c_event event)
{
P
Paul Brook 已提交
307
    WM8750State *s = (WM8750State *) i2c;
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

    switch (event) {
    case I2C_START_SEND:
        s->i2c_len = 0;
        break;
    case I2C_FINISH:
#ifdef VERBOSE
        if (s->i2c_len < 2)
            printf("%s: message too short (%i bytes)\n",
                            __FUNCTION__, s->i2c_len);
#endif
        break;
    default:
        break;
    }
}

#define WM8750_LINVOL	0x00
#define WM8750_RINVOL	0x01
#define WM8750_LOUT1V	0x02
#define WM8750_ROUT1V	0x03
#define WM8750_ADCDAC	0x05
#define WM8750_IFACE	0x07
#define WM8750_SRATE	0x08
#define WM8750_LDAC	0x0a
#define WM8750_RDAC	0x0b
#define WM8750_BASS	0x0c
#define WM8750_TREBLE	0x0d
#define WM8750_RESET	0x0f
#define WM8750_3D	0x10
#define WM8750_ALC1	0x11
#define WM8750_ALC2	0x12
#define WM8750_ALC3	0x13
#define WM8750_NGATE	0x14
#define WM8750_LADC	0x15
#define WM8750_RADC	0x16
#define WM8750_ADCTL1	0x17
#define WM8750_ADCTL2	0x18
#define WM8750_PWR1	0x19
#define WM8750_PWR2	0x1a
#define WM8750_ADCTL3	0x1b
#define WM8750_ADCIN	0x1f
#define WM8750_LADCIN	0x20
#define WM8750_RADCIN	0x21
#define WM8750_LOUTM1	0x22
#define WM8750_LOUTM2	0x23
#define WM8750_ROUTM1	0x24
#define WM8750_ROUTM2	0x25
#define WM8750_MOUTM1	0x26
#define WM8750_MOUTM2	0x27
#define WM8750_LOUT2V	0x28
#define WM8750_ROUT2V	0x29
#define WM8750_MOUTV	0x2a

static int wm8750_tx(i2c_slave *i2c, uint8_t data)
{
P
Paul Brook 已提交
364
    WM8750State *s = (WM8750State *) i2c;
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    uint8_t cmd;
    uint16_t value;

    if (s->i2c_len >= 2) {
        printf("%s: long message (%i bytes)\n", __FUNCTION__, s->i2c_len);
#ifdef VERBOSE
        return 1;
#endif
    }
    s->i2c_data[s->i2c_len ++] = data;
    if (s->i2c_len != 2)
        return 0;

    cmd = s->i2c_data[0] >> 1;
    value = ((s->i2c_data[0] << 8) | s->i2c_data[1]) & 0x1ff;

    switch (cmd) {
    case WM8750_LADCIN:	/* ADC Signal Path Control (Left) */
        s->diff[0] = (((value >> 6) & 3) == 3);	/* LINSEL */
        if (s->diff[0])
            s->in[0] = &s->adc_voice[0 + s->ds * 1];
        else
            s->in[0] = &s->adc_voice[((value >> 6) & 3) * 1 + 0];
        break;

    case WM8750_RADCIN:	/* ADC Signal Path Control (Right) */
        s->diff[1] = (((value >> 6) & 3) == 3);	/* RINSEL */
        if (s->diff[1])
            s->in[1] = &s->adc_voice[0 + s->ds * 1];
        else
            s->in[1] = &s->adc_voice[((value >> 6) & 3) * 1 + 0];
        break;

    case WM8750_ADCIN:	/* ADC Input Mode */
        s->ds = (value >> 8) & 1;	/* DS */
        if (s->diff[0])
            s->in[0] = &s->adc_voice[0 + s->ds * 1];
        if (s->diff[1])
            s->in[1] = &s->adc_voice[0 + s->ds * 1];
        s->monomix[0] = (value >> 6) & 3;	/* MONOMIX */
        break;

    case WM8750_ADCTL1:	/* Additional Control (1) */
        s->monomix[1] = (value >> 1) & 1;	/* DMONOMIX */
        break;

    case WM8750_PWR1:	/* Power Management (1) */
        s->enable = ((value >> 6) & 7) == 3;	/* VMIDSEL, VREF */
        wm8750_set_format(s);
        break;

    case WM8750_LINVOL:	/* Left Channel PGA */
        s->invol[0] = value & 0x3f;		/* LINVOL */
        s->inmute[0] = (value >> 7) & 1;	/* LINMUTE */
419
        wm8750_vol_update(s);
420 421 422 423 424
        break;

    case WM8750_RINVOL:	/* Right Channel PGA */
        s->invol[1] = value & 0x3f;		/* RINVOL */
        s->inmute[1] = (value >> 7) & 1;	/* RINMUTE */
425
        wm8750_vol_update(s);
426 427 428 429 430
        break;

    case WM8750_ADCDAC:	/* ADC and DAC Control */
        s->pol = (value >> 5) & 3;		/* ADCPOL */
        s->mute = (value >> 3) & 1;		/* DACMU */
431
        wm8750_vol_update(s);
432 433 434 435 436 437 438
        break;

    case WM8750_ADCTL3:	/* Additional Control (3) */
        break;

    case WM8750_LADC:	/* Left ADC Digital Volume */
        s->invol[2] = value & 0xff;		/* LADCVOL */
B
balrog 已提交
439
        wm8750_vol_update(s);
440 441 442 443
        break;

    case WM8750_RADC:	/* Right ADC Digital Volume */
        s->invol[3] = value & 0xff;		/* RADCVOL */
B
balrog 已提交
444
        wm8750_vol_update(s);
445 446 447 448 449 450 451 452 453 454 455 456
        break;

    case WM8750_ALC1:	/* ALC Control (1) */
        s->alc = (value >> 7) & 3;		/* ALCSEL */
        break;

    case WM8750_NGATE:	/* Noise Gate Control */
    case WM8750_3D:	/* 3D enhance */
        break;

    case WM8750_LDAC:	/* Left Channel Digital Volume */
        s->outvol[0] = value & 0xff;		/* LDACVOL */
B
balrog 已提交
457
        wm8750_vol_update(s);
458 459 460 461
        break;

    case WM8750_RDAC:	/* Right Channel Digital Volume */
        s->outvol[1] = value & 0xff;		/* RDACVOL */
B
balrog 已提交
462
        wm8750_vol_update(s);
463 464 465 466 467 468 469
        break;

    case WM8750_BASS:	/* Bass Control */
        break;

    case WM8750_LOUTM1:	/* Left Mixer Control (1) */
        s->path[0] = (value >> 8) & 1;		/* LD2LO */
B
balrog 已提交
470 471
        /* TODO: mute/unmute respective paths */
        wm8750_vol_update(s);
472 473 474 475
        break;

    case WM8750_LOUTM2:	/* Left Mixer Control (2) */
        s->path[1] = (value >> 8) & 1;		/* RD2LO */
B
balrog 已提交
476 477
        /* TODO: mute/unmute respective paths */
        wm8750_vol_update(s);
478 479 480 481
        break;

    case WM8750_ROUTM1:	/* Right Mixer Control (1) */
        s->path[2] = (value >> 8) & 1;		/* LD2RO */
B
balrog 已提交
482 483
        /* TODO: mute/unmute respective paths */
        wm8750_vol_update(s);
484 485 486 487
        break;

    case WM8750_ROUTM2:	/* Right Mixer Control (2) */
        s->path[3] = (value >> 8) & 1;		/* RD2RO */
B
balrog 已提交
488 489
        /* TODO: mute/unmute respective paths */
        wm8750_vol_update(s);
490 491 492 493
        break;

    case WM8750_MOUTM1:	/* Mono Mixer Control (1) */
        s->mpath[0] = (value >> 8) & 1;		/* LD2MO */
B
balrog 已提交
494 495
        /* TODO: mute/unmute respective paths */
        wm8750_vol_update(s);
496 497 498 499
        break;

    case WM8750_MOUTM2:	/* Mono Mixer Control (2) */
        s->mpath[1] = (value >> 8) & 1;		/* RD2MO */
B
balrog 已提交
500 501
        /* TODO: mute/unmute respective paths */
        wm8750_vol_update(s);
502 503 504
        break;

    case WM8750_LOUT1V:	/* LOUT1 Volume */
505
        s->outvol[2] = value & 0x7f;		/* LOUT1VOL */
B
balrog 已提交
506
        wm8750_vol_update(s);
507 508 509 510
        break;

    case WM8750_LOUT2V:	/* LOUT2 Volume */
        s->outvol[4] = value & 0x7f;		/* LOUT2VOL */
511
        wm8750_vol_update(s);
512 513 514
        break;

    case WM8750_ROUT1V:	/* ROUT1 Volume */
515
        s->outvol[3] = value & 0x7f;		/* ROUT1VOL */
B
balrog 已提交
516
        wm8750_vol_update(s);
517 518 519 520
        break;

    case WM8750_ROUT2V:	/* ROUT2 Volume */
        s->outvol[5] = value & 0x7f;		/* ROUT2VOL */
521
        wm8750_vol_update(s);
522 523 524 525
        break;

    case WM8750_MOUTV:	/* MONOOUT Volume */
        s->outvol[6] = value & 0x7f;		/* MONOOUTVOL */
B
balrog 已提交
526
        wm8750_vol_update(s);
527 528 529 530 531 532 533
        break;

    case WM8750_ADCTL2:	/* Additional Control (2) */
        break;

    case WM8750_PWR2:	/* Power Management (2) */
        s->power = value & 0x7e;
B
balrog 已提交
534 535
        /* TODO: mute/unmute respective paths */
        wm8750_vol_update(s);
536 537 538 539
        break;

    case WM8750_IFACE:	/* Digital Audio Interface Format */
        s->format = value;
540 541
        s->master = (value >> 6) & 1;			/* MS */
        wm8750_clk_update(s, s->master);
542 543 544 545
        break;

    case WM8750_SRATE:	/* Clocking and Sample Rate Control */
        s->rate = &wm_rate_table[(value >> 1) & 0x1f];
546
        wm8750_clk_update(s, 0);
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
        break;

    case WM8750_RESET:	/* Reset */
        wm8750_reset(&s->i2c);
        break;

#ifdef VERBOSE
    default:
        printf("%s: unknown register %02x\n", __FUNCTION__, cmd);
#endif
    }

    return 0;
}

static int wm8750_rx(i2c_slave *i2c)
{
    return 0x00;
}

567 568
static void wm8750_save(QEMUFile *f, void *opaque)
{
P
Paul Brook 已提交
569
    WM8750State *s = (WM8750State *) opaque;
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    int i;
    qemu_put_8s(f, &s->i2c_data[0]);
    qemu_put_8s(f, &s->i2c_data[1]);
    qemu_put_be32(f, s->i2c_len);
    qemu_put_be32(f, s->enable);
    qemu_put_be32(f, s->idx_in);
    qemu_put_be32(f, s->req_in);
    qemu_put_be32(f, s->idx_out);
    qemu_put_be32(f, s->req_out);

    for (i = 0; i < 7; i ++)
        qemu_put_8s(f, &s->outvol[i]);
    for (i = 0; i < 2; i ++)
        qemu_put_8s(f, &s->outmute[i]);
    for (i = 0; i < 4; i ++)
        qemu_put_8s(f, &s->invol[i]);
    for (i = 0; i < 2; i ++)
        qemu_put_8s(f, &s->inmute[i]);

    for (i = 0; i < 2; i ++)
        qemu_put_8s(f, &s->diff[i]);
    qemu_put_8s(f, &s->pol);
    qemu_put_8s(f, &s->ds);
    for (i = 0; i < 2; i ++)
        qemu_put_8s(f, &s->monomix[i]);
    qemu_put_8s(f, &s->alc);
    qemu_put_8s(f, &s->mute);
    for (i = 0; i < 4; i ++)
        qemu_put_8s(f, &s->path[i]);
    for (i = 0; i < 2; i ++)
        qemu_put_8s(f, &s->mpath[i]);
    qemu_put_8s(f, &s->format);
    qemu_put_8s(f, &s->power);
    qemu_put_byte(f, (s->rate - wm_rate_table) / sizeof(*s->rate));
    i2c_slave_save(f, &s->i2c);
}

static int wm8750_load(QEMUFile *f, void *opaque, int version_id)
{
P
Paul Brook 已提交
609
    WM8750State *s = (WM8750State *) opaque;
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
    int i;
    qemu_get_8s(f, &s->i2c_data[0]);
    qemu_get_8s(f, &s->i2c_data[1]);
    s->i2c_len = qemu_get_be32(f);
    s->enable = qemu_get_be32(f);
    s->idx_in = qemu_get_be32(f);
    s->req_in = qemu_get_be32(f);
    s->idx_out = qemu_get_be32(f);
    s->req_out = qemu_get_be32(f);

    for (i = 0; i < 7; i ++)
        qemu_get_8s(f, &s->outvol[i]);
    for (i = 0; i < 2; i ++)
        qemu_get_8s(f, &s->outmute[i]);
    for (i = 0; i < 4; i ++)
        qemu_get_8s(f, &s->invol[i]);
    for (i = 0; i < 2; i ++)
        qemu_get_8s(f, &s->inmute[i]);

    for (i = 0; i < 2; i ++)
        qemu_get_8s(f, &s->diff[i]);
    qemu_get_8s(f, &s->pol);
    qemu_get_8s(f, &s->ds);
    for (i = 0; i < 2; i ++)
        qemu_get_8s(f, &s->monomix[i]);
    qemu_get_8s(f, &s->alc);
    qemu_get_8s(f, &s->mute);
    for (i = 0; i < 4; i ++)
        qemu_get_8s(f, &s->path[i]);
    for (i = 0; i < 2; i ++)
        qemu_get_8s(f, &s->mpath[i]);
    qemu_get_8s(f, &s->format);
    qemu_get_8s(f, &s->power);
    s->rate = &wm_rate_table[(uint8_t) qemu_get_byte(f) & 0x1f];
    i2c_slave_load(f, &s->i2c);
    return 0;
}

648
static int wm8750_init(i2c_slave *i2c)
649
{
P
Paul Brook 已提交
650
    WM8750State *s = FROM_I2C_SLAVE(WM8750State, i2c);
651

652
    AUD_register_card(CODEC, &s->card);
653 654
    wm8750_reset(&s->i2c);

655
    register_savevm(CODEC, -1, 0, wm8750_save, wm8750_load, s);
656
    return 0;
657 658
}

B
balrog 已提交
659
#if 0
660
static void wm8750_fini(i2c_slave *i2c)
661
{
P
Paul Brook 已提交
662
    WM8750State *s = (WM8750State *) i2c;
663 664 665 666
    wm8750_reset(&s->i2c);
    AUD_remove_card(&s->card);
    qemu_free(s);
}
B
balrog 已提交
667
#endif
668

P
Paul Brook 已提交
669
void wm8750_data_req_set(DeviceState *dev,
670 671
                void (*data_req)(void *, int, int), void *opaque)
{
P
Paul Brook 已提交
672
    WM8750State *s = FROM_I2C_SLAVE(WM8750State, I2C_SLAVE_FROM_QDEV(dev));
673 674 675 676 677 678
    s->data_req = data_req;
    s->opaque = opaque;
}

void wm8750_dac_dat(void *opaque, uint32_t sample)
{
P
Paul Brook 已提交
679
    WM8750State *s = (WM8750State *) opaque;
680

681
    *(uint32_t *) &s->data_out[s->idx_out] = sample;
682 683 684 685 686 687
    s->req_out -= 4;
    s->idx_out += 4;
    if (s->idx_out >= sizeof(s->data_out) || s->req_out <= 0)
        wm8750_out_flush(s);
}

688 689
void *wm8750_dac_buffer(void *opaque, int samples)
{
P
Paul Brook 已提交
690
    WM8750State *s = (WM8750State *) opaque;
691 692 693 694 695 696 697 698 699 700
    /* XXX: Should check if there are <i>samples</i> free samples available */
    void *ret = s->data_out + s->idx_out;

    s->idx_out += samples << 2;
    s->req_out -= samples << 2;
    return ret;
}

void wm8750_dac_commit(void *opaque)
{
P
Paul Brook 已提交
701
    WM8750State *s = (WM8750State *) opaque;
702

703
    wm8750_out_flush(s);
704 705
}

706 707
uint32_t wm8750_adc_dat(void *opaque)
{
P
Paul Brook 已提交
708
    WM8750State *s = (WM8750State *) opaque;
709
    uint32_t *data;
710

711 712
    if (s->idx_in >= sizeof(s->data_in))
        wm8750_in_load(s);
713

714 715 716
    data = (uint32_t *) &s->data_in[s->idx_in];
    s->req_in -= 4;
    s->idx_in += 4;
717
    return *data;
718
}
719

720
void wm8750_set_bclk_in(void *opaque, int new_hz)
721
{
P
Paul Brook 已提交
722
    WM8750State *s = (WM8750State *) opaque;
723

724 725
    s->ext_adc_hz = new_hz;
    s->ext_dac_hz = new_hz;
726 727
    wm8750_clk_update(s, 1);
}
P
Paul Brook 已提交
728 729

static I2CSlaveInfo wm8750_info = {
730 731
    .qdev.name = "wm8750",
    .qdev.size = sizeof(WM8750State),
P
Paul Brook 已提交
732 733 734 735 736 737 738 739
    .init = wm8750_init,
    .event = wm8750_event,
    .recv = wm8750_rx,
    .send = wm8750_tx
};

static void wm8750_register_devices(void)
{
740
    i2c_register_slave(&wm8750_info);
P
Paul Brook 已提交
741 742 743
}

device_init(wm8750_register_devices)