Statistics
| Branch: | Revision:

root / hw / wm8750.c @ b0f74c87

History | View | Annotate | Download (21 kB)

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 "hw.h"
11
#include "i2c.h"
12
#include "audio/audio.h"
13

    
14
#define IN_PORT_N        3
15
#define OUT_PORT_N        3
16

    
17
#define CODEC                "wm8750"
18

    
19
struct wm_rate_s;
20
struct wm8750_s {
21
    i2c_slave i2c;
22
    uint8_t i2c_data[2];
23
    int i2c_len;
24
    QEMUSoundCard card;
25
    SWVoiceIn *adc_voice[IN_PORT_N];
26
    SWVoiceOut *dac_voice[OUT_PORT_N];
27
    int enable;
28
    void (*data_req)(void *, int, int);
29
    void *opaque;
30
    uint8_t data_in[4096];
31
    uint8_t data_out[4096];
32
    int idx_in, req_in;
33
    int idx_out, req_out;
34

    
35
    SWVoiceOut **out[2];
36
    uint8_t outvol[7], outmute[2];
37
    SWVoiceIn **in[2];
38
    uint8_t invol[4], inmute[2];
39

    
40
    uint8_t diff[2], pol, ds, monomix[2], alc, mute;
41
    uint8_t path[4], mpath[2], power, format;
42
    const struct wm_rate_s *rate;
43
    int adc_hz, dac_hz, ext_adc_hz, ext_dac_hz, master;
44
};
45

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

    
53
#define WM8750_OUTVOL_TRANSFORM(x)        wm8750_vol_db_table[(0x7f - x) / 3]
54
#define WM8750_INVOL_TRANSFORM(x)        (x << 2)
55

    
56
static inline void wm8750_in_load(struct wm8750_s *s)
57
{
58
    int acquired;
59
    if (s->idx_in + s->req_in <= sizeof(s->data_in))
60
        return;
61
    s->idx_in = audio_MAX(0, (int) sizeof(s->data_in) - s->req_in);
62
    acquired = AUD_read(*s->in[0], s->data_in + s->idx_in,
63
                    sizeof(s->data_in) - s->idx_in);
64
}
65

    
66
static inline void wm8750_out_flush(struct wm8750_s *s)
67
{
68
    int sent = 0;
69
    while (sent < s->idx_out)
70
        sent += AUD_write(*s->out[0], s->data_out + sent, s->idx_out - sent)
71
                ?: s->idx_out;
72
    s->idx_out = 0;
73
}
74

    
75
static void wm8750_audio_in_cb(void *opaque, int avail_b)
76
{
77
    struct wm8750_s *s = (struct wm8750_s *) opaque;
78
    s->req_in = avail_b;
79
    s->data_req(s->opaque, s->req_out >> 2, avail_b >> 2);
80
}
81

    
82
static void wm8750_audio_out_cb(void *opaque, int free_b)
83
{
84
    struct wm8750_s *s = (struct wm8750_s *) opaque;
85

    
86
    if (s->idx_out >= free_b) {
87
        s->idx_out = free_b;
88
        s->req_out = 0;
89
        wm8750_out_flush(s);
90
    } else
91
        s->req_out = free_b - s->idx_out;
92
 
93
    s->data_req(s->opaque, s->req_out >> 2, s->req_in >> 2);
94
}
95

    
96
struct wm_rate_s {
97
    int adc;
98
    int adc_hz;
99
    int dac;
100
    int dac_hz;
101
};
102

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

    
138
static void wm8750_vol_update(struct wm8750_s *s)
139
{
140
    /* FIXME: multiply all volumes by s->invol[2], s->invol[3] */
141

    
142
    AUD_set_volume_in(s->adc_voice[0], s->mute,
143
                    s->inmute[0] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[0]),
144
                    s->inmute[1] ? 0 : WM8750_INVOL_TRANSFORM(s->invol[1]));
145
    AUD_set_volume_in(s->adc_voice[1], 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[2], 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

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

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

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

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

    
170
static void wm8750_set_format(struct wm8750_s *s)
171
{
172
    int i;
173
    audsettings_t in_fmt;
174
    audsettings_t out_fmt;
175
    audsettings_t monoout_fmt;
176

    
177
    wm8750_out_flush(s);
178

    
179
    if (s->in[0] && *s->in[0])
180
        AUD_set_active_in(*s->in[0], 0);
181
    if (s->out[0] && *s->out[0])
182
        AUD_set_active_out(*s->out[0], 0);
183

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

    
195
    if (!s->enable)
196
        return;
197

    
198
    /* Setup input */
199
    in_fmt.endianness = 0;
200
    in_fmt.nchannels = 2;
201
    in_fmt.freq = s->adc_hz;
202
    in_fmt.fmt = AUD_FMT_S16;
203

    
204
    s->adc_voice[0] = AUD_open_in(&s->card, s->adc_voice[0],
205
                    CODEC ".input1", s, wm8750_audio_in_cb, &in_fmt);
206
    s->adc_voice[1] = AUD_open_in(&s->card, s->adc_voice[1],
207
                    CODEC ".input2", s, wm8750_audio_in_cb, &in_fmt);
208
    s->adc_voice[2] = AUD_open_in(&s->card, s->adc_voice[2],
209
                    CODEC ".input3", s, wm8750_audio_in_cb, &in_fmt);
210

    
211
    /* Setup output */
212
    out_fmt.endianness = 0;
213
    out_fmt.nchannels = 2;
214
    out_fmt.freq = s->dac_hz;
215
    out_fmt.fmt = AUD_FMT_S16;
216
    monoout_fmt.endianness = 0;
217
    monoout_fmt.nchannels = 1;
218
    monoout_fmt.freq = s->rate->dac_hz;
219
    monoout_fmt.fmt = AUD_FMT_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

    
242
static void wm8750_clk_update(struct wm8750_s *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

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

    
306
static void wm8750_event(i2c_slave *i2c, enum i2c_event event)
307
{
308
    struct wm8750_s *s = (struct wm8750_s *) i2c;
309

    
310
    switch (event) {
311
    case I2C_START_SEND:
312
        s->i2c_len = 0;
313
        break;
314
    case I2C_FINISH:
315
#ifdef VERBOSE
316
        if (s->i2c_len < 2)
317
            printf("%s: message too short (%i bytes)\n",
318
                            __FUNCTION__, s->i2c_len);
319
#endif
320
        break;
321
    default:
322
        break;
323
    }
324
}
325

    
326
#define WM8750_LINVOL        0x00
327
#define WM8750_RINVOL        0x01
328
#define WM8750_LOUT1V        0x02
329
#define WM8750_ROUT1V        0x03
330
#define WM8750_ADCDAC        0x05
331
#define WM8750_IFACE        0x07
332
#define WM8750_SRATE        0x08
333
#define WM8750_LDAC        0x0a
334
#define WM8750_RDAC        0x0b
335
#define WM8750_BASS        0x0c
336
#define WM8750_TREBLE        0x0d
337
#define WM8750_RESET        0x0f
338
#define WM8750_3D        0x10
339
#define WM8750_ALC1        0x11
340
#define WM8750_ALC2        0x12
341
#define WM8750_ALC3        0x13
342
#define WM8750_NGATE        0x14
343
#define WM8750_LADC        0x15
344
#define WM8750_RADC        0x16
345
#define WM8750_ADCTL1        0x17
346
#define WM8750_ADCTL2        0x18
347
#define WM8750_PWR1        0x19
348
#define WM8750_PWR2        0x1a
349
#define WM8750_ADCTL3        0x1b
350
#define WM8750_ADCIN        0x1f
351
#define WM8750_LADCIN        0x20
352
#define WM8750_RADCIN        0x21
353
#define WM8750_LOUTM1        0x22
354
#define WM8750_LOUTM2        0x23
355
#define WM8750_ROUTM1        0x24
356
#define WM8750_ROUTM2        0x25
357
#define WM8750_MOUTM1        0x26
358
#define WM8750_MOUTM2        0x27
359
#define WM8750_LOUT2V        0x28
360
#define WM8750_ROUT2V        0x29
361
#define WM8750_MOUTV        0x2a
362

    
363
static int wm8750_tx(i2c_slave *i2c, uint8_t data)
364
{
365
    struct wm8750_s *s = (struct wm8750_s *) i2c;
366
    uint8_t cmd;
367
    uint16_t value;
368

    
369
    if (s->i2c_len >= 2) {
370
        printf("%s: long message (%i bytes)\n", __FUNCTION__, s->i2c_len);
371
#ifdef VERBOSE
372
        return 1;
373
#endif
374
    }
375
    s->i2c_data[s->i2c_len ++] = data;
376
    if (s->i2c_len != 2)
377
        return 0;
378

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

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

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

    
399
    case WM8750_ADCIN:        /* ADC Input Mode */
400
        s->ds = (value >> 8) & 1;        /* DS */
401
        if (s->diff[0])
402
            s->in[0] = &s->adc_voice[0 + s->ds * 1];
403
        if (s->diff[1])
404
            s->in[1] = &s->adc_voice[0 + s->ds * 1];
405
        s->monomix[0] = (value >> 6) & 3;        /* MONOMIX */
406
        break;
407

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

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

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

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

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

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

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

    
443
    case WM8750_RADC:        /* Right ADC Digital Volume */
444
        s->invol[3] = value & 0xff;                /* RADCVOL */
445
        wm8750_vol_update(s);
446
        break;
447

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

    
452
    case WM8750_NGATE:        /* Noise Gate Control */
453
    case WM8750_3D:        /* 3D enhance */
454
        break;
455

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

    
461
    case WM8750_RDAC:        /* Right Channel Digital Volume */
462
        s->outvol[1] = value & 0xff;                /* RDACVOL */
463
        wm8750_vol_update(s);
464
        break;
465

    
466
    case WM8750_BASS:        /* Bass Control */
467
        break;
468

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

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

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

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

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

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

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

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

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

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

    
525
    case WM8750_MOUTV:        /* MONOOUT Volume */
526
        s->outvol[6] = value & 0x7f;                /* MONOOUTVOL */
527
        wm8750_vol_update(s);
528
        break;
529

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

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

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

    
545
    case WM8750_SRATE:        /* Clocking and Sample Rate Control */
546
        s->rate = &wm_rate_table[(value >> 1) & 0x1f];
547
        wm8750_clk_update(s, 0);
548
        break;
549

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

    
554
#ifdef VERBOSE
555
    default:
556
        printf("%s: unknown register %02x\n", __FUNCTION__, cmd);
557
#endif
558
    }
559

    
560
    return 0;
561
}
562

    
563
static int wm8750_rx(i2c_slave *i2c)
564
{
565
    return 0x00;
566
}
567

    
568
static void wm8750_save(QEMUFile *f, void *opaque)
569
{
570
    struct wm8750_s *s = (struct wm8750_s *) opaque;
571
    int i;
572
    qemu_put_8s(f, &s->i2c_data[0]);
573
    qemu_put_8s(f, &s->i2c_data[1]);
574
    qemu_put_be32(f, s->i2c_len);
575
    qemu_put_be32(f, s->enable);
576
    qemu_put_be32(f, s->idx_in);
577
    qemu_put_be32(f, s->req_in);
578
    qemu_put_be32(f, s->idx_out);
579
    qemu_put_be32(f, s->req_out);
580

    
581
    for (i = 0; i < 7; i ++)
582
        qemu_put_8s(f, &s->outvol[i]);
583
    for (i = 0; i < 2; i ++)
584
        qemu_put_8s(f, &s->outmute[i]);
585
    for (i = 0; i < 4; i ++)
586
        qemu_put_8s(f, &s->invol[i]);
587
    for (i = 0; i < 2; i ++)
588
        qemu_put_8s(f, &s->inmute[i]);
589

    
590
    for (i = 0; i < 2; i ++)
591
        qemu_put_8s(f, &s->diff[i]);
592
    qemu_put_8s(f, &s->pol);
593
    qemu_put_8s(f, &s->ds);
594
    for (i = 0; i < 2; i ++)
595
        qemu_put_8s(f, &s->monomix[i]);
596
    qemu_put_8s(f, &s->alc);
597
    qemu_put_8s(f, &s->mute);
598
    for (i = 0; i < 4; i ++)
599
        qemu_put_8s(f, &s->path[i]);
600
    for (i = 0; i < 2; i ++)
601
        qemu_put_8s(f, &s->mpath[i]);
602
    qemu_put_8s(f, &s->format);
603
    qemu_put_8s(f, &s->power);
604
    qemu_put_byte(f, (s->rate - wm_rate_table) / sizeof(*s->rate));
605
    i2c_slave_save(f, &s->i2c);
606
}
607

    
608
static int wm8750_load(QEMUFile *f, void *opaque, int version_id)
609
{
610
    struct wm8750_s *s = (struct wm8750_s *) opaque;
611
    int i;
612
    qemu_get_8s(f, &s->i2c_data[0]);
613
    qemu_get_8s(f, &s->i2c_data[1]);
614
    s->i2c_len = qemu_get_be32(f);
615
    s->enable = qemu_get_be32(f);
616
    s->idx_in = qemu_get_be32(f);
617
    s->req_in = qemu_get_be32(f);
618
    s->idx_out = qemu_get_be32(f);
619
    s->req_out = qemu_get_be32(f);
620

    
621
    for (i = 0; i < 7; i ++)
622
        qemu_get_8s(f, &s->outvol[i]);
623
    for (i = 0; i < 2; i ++)
624
        qemu_get_8s(f, &s->outmute[i]);
625
    for (i = 0; i < 4; i ++)
626
        qemu_get_8s(f, &s->invol[i]);
627
    for (i = 0; i < 2; i ++)
628
        qemu_get_8s(f, &s->inmute[i]);
629

    
630
    for (i = 0; i < 2; i ++)
631
        qemu_get_8s(f, &s->diff[i]);
632
    qemu_get_8s(f, &s->pol);
633
    qemu_get_8s(f, &s->ds);
634
    for (i = 0; i < 2; i ++)
635
        qemu_get_8s(f, &s->monomix[i]);
636
    qemu_get_8s(f, &s->alc);
637
    qemu_get_8s(f, &s->mute);
638
    for (i = 0; i < 4; i ++)
639
        qemu_get_8s(f, &s->path[i]);
640
    for (i = 0; i < 2; i ++)
641
        qemu_get_8s(f, &s->mpath[i]);
642
    qemu_get_8s(f, &s->format);
643
    qemu_get_8s(f, &s->power);
644
    s->rate = &wm_rate_table[(uint8_t) qemu_get_byte(f) & 0x1f];
645
    i2c_slave_load(f, &s->i2c);
646
    return 0;
647
}
648

    
649
i2c_slave *wm8750_init(i2c_bus *bus, AudioState *audio)
650
{
651
    struct wm8750_s *s = (struct wm8750_s *)
652
            i2c_slave_init(bus, 0, sizeof(struct wm8750_s));
653
    s->i2c.event = wm8750_event;
654
    s->i2c.recv = wm8750_rx;
655
    s->i2c.send = wm8750_tx;
656

    
657
    AUD_register_card(audio, CODEC, &s->card);
658
    wm8750_reset(&s->i2c);
659

    
660
    register_savevm(CODEC, -1, 0, wm8750_save, wm8750_load, s);
661

    
662
    return &s->i2c;
663
}
664

    
665
#if 0
666
static void wm8750_fini(i2c_slave *i2c)
667
{
668
    struct wm8750_s *s = (struct wm8750_s *) i2c;
669
    wm8750_reset(&s->i2c);
670
    AUD_remove_card(&s->card);
671
    qemu_free(s);
672
}
673
#endif
674

    
675
void wm8750_data_req_set(i2c_slave *i2c,
676
                void (*data_req)(void *, int, int), void *opaque)
677
{
678
    struct wm8750_s *s = (struct wm8750_s *) i2c;
679
    s->data_req = data_req;
680
    s->opaque = opaque;
681
}
682

    
683
void wm8750_dac_dat(void *opaque, uint32_t sample)
684
{
685
    struct wm8750_s *s = (struct wm8750_s *) opaque;
686

    
687
    *(uint32_t *) &s->data_out[s->idx_out] = sample;
688
    s->req_out -= 4;
689
    s->idx_out += 4;
690
    if (s->idx_out >= sizeof(s->data_out) || s->req_out <= 0)
691
        wm8750_out_flush(s);
692
}
693

    
694
void *wm8750_dac_buffer(void *opaque, int samples)
695
{
696
    struct wm8750_s *s = (struct wm8750_s *) opaque;
697
    /* XXX: Should check if there are <i>samples</i> free samples available */
698
    void *ret = s->data_out + s->idx_out;
699

    
700
    s->idx_out += samples << 2;
701
    s->req_out -= samples << 2;
702
    return ret;
703
}
704

    
705
void wm8750_dac_commit(void *opaque)
706
{
707
    struct wm8750_s *s = (struct wm8750_s *) opaque;
708

    
709
    return wm8750_out_flush(s);
710
}
711

    
712
uint32_t wm8750_adc_dat(void *opaque)
713
{
714
    struct wm8750_s *s = (struct wm8750_s *) opaque;
715
    uint32_t *data;
716

    
717
    if (s->idx_in >= sizeof(s->data_in))
718
        wm8750_in_load(s);
719

    
720
    data = (uint32_t *) &s->data_in[s->idx_in];
721
    s->req_in -= 4;
722
    s->idx_in += 4;
723
    return *data;
724
}
725

    
726
void wm8750_set_bclk_in(void *opaque, int new_hz)
727
{
728
    struct wm8750_s *s = (struct wm8750_s *) opaque;
729

    
730
    s->ext_adc_hz = new_hz;
731
    s->ext_dac_hz = new_hz;
732
    wm8750_clk_update(s, 1);
733
}