| 1 | /* |
| 2 | * LM4549 Audio Codec Interface |
| 3 | * |
| 4 | * Copyright (c) 2011 |
| 5 | * Written by Mathieu Sonet - www.elasticsheep.com |
| 6 | * |
| 7 | * This code is licensed under the GPL. |
| 8 | * |
| 9 | * ***************************************************************** |
| 10 | * |
| 11 | * This driver emulates the LM4549 codec. |
| 12 | * |
| 13 | * It supports only one playback voice and no record voice. |
| 14 | */ |
| 15 | |
| 16 | #include "qemu/osdep.h" |
| 17 | #include "hw/core/hw-error.h" |
| 18 | #include "qemu/log.h" |
| 19 | #include "qemu/audio.h" |
| 20 | #include "lm4549.h" |
| 21 | #include "migration/vmstate.h" |
| 22 | |
| 23 | #if 0 |
| 24 | #define LM4549_DEBUG 1 |
| 25 | #endif |
| 26 | |
| 27 | #if 0 |
| 28 | #define LM4549_DUMP_DAC_INPUT 1 |
| 29 | #endif |
| 30 | |
| 31 | #ifdef LM4549_DEBUG |
| 32 | #define DPRINTF(fmt, ...) \ |
| 33 | do { printf("lm4549: " fmt , ## __VA_ARGS__); } while (0) |
| 34 | #else |
| 35 | #define DPRINTF(fmt, ...) do {} while (0) |
| 36 | #endif |
| 37 | |
| 38 | #if defined(LM4549_DUMP_DAC_INPUT) |
| 39 | static FILE *fp_dac_input; |
| 40 | #endif |
| 41 | |
| 42 | /* LM4549 register list */ |
| 43 | enum { |
| 44 | LM4549_Reset = 0x00, |
| 45 | LM4549_Master_Volume = 0x02, |
| 46 | LM4549_Line_Out_Volume = 0x04, |
| 47 | LM4549_Master_Volume_Mono = 0x06, |
| 48 | LM4549_PC_Beep_Volume = 0x0A, |
| 49 | LM4549_Phone_Volume = 0x0C, |
| 50 | LM4549_Mic_Volume = 0x0E, |
| 51 | LM4549_Line_In_Volume = 0x10, |
| 52 | LM4549_CD_Volume = 0x12, |
| 53 | LM4549_Video_Volume = 0x14, |
| 54 | LM4549_Aux_Volume = 0x16, |
| 55 | LM4549_PCM_Out_Volume = 0x18, |
| 56 | LM4549_Record_Select = 0x1A, |
| 57 | LM4549_Record_Gain = 0x1C, |
| 58 | LM4549_General_Purpose = 0x20, |
| 59 | LM4549_3D_Control = 0x22, |
| 60 | LM4549_Powerdown_Ctrl_Stat = 0x26, |
| 61 | LM4549_Ext_Audio_ID = 0x28, |
| 62 | LM4549_Ext_Audio_Stat_Ctrl = 0x2A, |
| 63 | LM4549_PCM_Front_DAC_Rate = 0x2C, |
| 64 | LM4549_PCM_ADC_Rate = 0x32, |
| 65 | LM4549_Vendor_ID1 = 0x7C, |
| 66 | LM4549_Vendor_ID2 = 0x7E |
| 67 | }; |
| 68 | |
| 69 | static void lm4549_reset(lm4549_state *s) |
| 70 | { |
| 71 | uint16_t *regfile = s->regfile; |
| 72 | |
| 73 | regfile[LM4549_Reset] = 0x0d50; |
| 74 | regfile[LM4549_Master_Volume] = 0x8008; |
| 75 | regfile[LM4549_Line_Out_Volume] = 0x8000; |
| 76 | regfile[LM4549_Master_Volume_Mono] = 0x8000; |
| 77 | regfile[LM4549_PC_Beep_Volume] = 0x0000; |
| 78 | regfile[LM4549_Phone_Volume] = 0x8008; |
| 79 | regfile[LM4549_Mic_Volume] = 0x8008; |
| 80 | regfile[LM4549_Line_In_Volume] = 0x8808; |
| 81 | regfile[LM4549_CD_Volume] = 0x8808; |
| 82 | regfile[LM4549_Video_Volume] = 0x8808; |
| 83 | regfile[LM4549_Aux_Volume] = 0x8808; |
| 84 | regfile[LM4549_PCM_Out_Volume] = 0x8808; |
| 85 | regfile[LM4549_Record_Select] = 0x0000; |
| 86 | regfile[LM4549_Record_Gain] = 0x8000; |
| 87 | regfile[LM4549_General_Purpose] = 0x0000; |
| 88 | regfile[LM4549_3D_Control] = 0x0101; |
| 89 | regfile[LM4549_Powerdown_Ctrl_Stat] = 0x000f; |
| 90 | regfile[LM4549_Ext_Audio_ID] = 0x0001; |
| 91 | regfile[LM4549_Ext_Audio_Stat_Ctrl] = 0x0000; |
| 92 | regfile[LM4549_PCM_Front_DAC_Rate] = 0xbb80; |
| 93 | regfile[LM4549_PCM_ADC_Rate] = 0xbb80; |
| 94 | regfile[LM4549_Vendor_ID1] = 0x4e53; |
| 95 | regfile[LM4549_Vendor_ID2] = 0x4331; |
| 96 | } |
| 97 | |
| 98 | static void lm4549_audio_transfer(lm4549_state *s) |
| 99 | { |
| 100 | uint32_t written_bytes, written_samples; |
| 101 | uint32_t i; |
| 102 | |
| 103 | /* Activate the voice */ |
| 104 | audio_be_set_active_out(s->audio_be, s->voice, 1); |
| 105 | s->voice_is_active = 1; |
| 106 | |
| 107 | /* Try to write the buffer content */ |
| 108 | written_bytes = audio_be_write(s->audio_be, s->voice, s->buffer, |
| 109 | s->buffer_level * sizeof(uint16_t)); |
| 110 | written_samples = written_bytes >> 1; |
| 111 | |
| 112 | #if defined(LM4549_DUMP_DAC_INPUT) |
| 113 | fwrite(s->buffer, sizeof(uint8_t), written_bytes, fp_dac_input); |
| 114 | #endif |
| 115 | |
| 116 | s->buffer_level -= written_samples; |
| 117 | |
| 118 | if (s->buffer_level > 0) { |
| 119 | /* Move the data back to the start of the buffer */ |
| 120 | for (i = 0; i < s->buffer_level; i++) { |
| 121 | s->buffer[i] = s->buffer[i + written_samples]; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | static void lm4549_audio_out_callback(void *opaque, int free) |
| 127 | { |
| 128 | lm4549_state *s = (lm4549_state *)opaque; |
| 129 | static uint32_t prev_buffer_level; |
| 130 | |
| 131 | #ifdef LM4549_DEBUG |
| 132 | int size = audio_be_get_buffer_size_out(s->audio_be, s->voice); |
| 133 | DPRINTF("audio_out_callback size = %i free = %i\n", size, free); |
| 134 | #endif |
| 135 | |
| 136 | /* Detect that no data are consumed |
| 137 | => disable the voice */ |
| 138 | if (s->buffer_level == prev_buffer_level) { |
| 139 | audio_be_set_active_out(s->audio_be, s->voice, 0); |
| 140 | s->voice_is_active = 0; |
| 141 | } |
| 142 | prev_buffer_level = s->buffer_level; |
| 143 | |
| 144 | /* Check if a buffer transfer is pending */ |
| 145 | if (s->buffer_level == LM4549_BUFFER_SIZE) { |
| 146 | lm4549_audio_transfer(s); |
| 147 | |
| 148 | /* Request more data */ |
| 149 | if (s->data_req_cb != NULL) { |
| 150 | (s->data_req_cb)(s->opaque); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | uint32_t lm4549_read(lm4549_state *s, hwaddr offset) |
| 156 | { |
| 157 | uint16_t *regfile = s->regfile; |
| 158 | uint32_t value = 0; |
| 159 | |
| 160 | /* Read the stored value */ |
| 161 | assert(offset < 128); |
| 162 | value = regfile[offset]; |
| 163 | |
| 164 | DPRINTF("read [0x%02x] = 0x%04x\n", offset, value); |
| 165 | |
| 166 | return value; |
| 167 | } |
| 168 | |
| 169 | void lm4549_write(lm4549_state *s, |
| 170 | hwaddr offset, uint32_t value) |
| 171 | { |
| 172 | uint16_t *regfile = s->regfile; |
| 173 | |
| 174 | assert(offset < 128); |
| 175 | DPRINTF("write [0x%02x] = 0x%04x\n", offset, value); |
| 176 | |
| 177 | switch (offset) { |
| 178 | case LM4549_Reset: |
| 179 | lm4549_reset(s); |
| 180 | break; |
| 181 | |
| 182 | case LM4549_PCM_Front_DAC_Rate: |
| 183 | DPRINTF("DAC rate change = %i\n", value); |
| 184 | |
| 185 | /* |
| 186 | * Valid sample rates are 4kHz to 48kHz. |
| 187 | * The datasheet doesn't say what happens if you try to |
| 188 | * set the frequency to zero. AUD_open_out() will print |
| 189 | * a bug message if we pass it a zero frequency, so just |
| 190 | * ignore attempts to set the DAC frequency to zero. |
| 191 | */ |
| 192 | if (value < 4000 || value > 48000) { |
| 193 | qemu_log_mask(LOG_GUEST_ERROR, |
| 194 | "%s: DAC sample rate %d Hz is invalid, ignoring it\n", |
| 195 | __func__, value); |
| 196 | break; |
| 197 | } |
| 198 | regfile[LM4549_PCM_Front_DAC_Rate] = value; |
| 199 | |
| 200 | /* Re-open a voice with the new sample rate */ |
| 201 | struct audsettings as; |
| 202 | as.freq = value; |
| 203 | as.nchannels = 2; |
| 204 | as.fmt = AUDIO_FORMAT_S16; |
| 205 | as.big_endian = false; |
| 206 | |
| 207 | s->voice = audio_be_open_out( |
| 208 | s->audio_be, |
| 209 | s->voice, |
| 210 | "lm4549.out", |
| 211 | s, |
| 212 | lm4549_audio_out_callback, |
| 213 | &as |
| 214 | ); |
| 215 | break; |
| 216 | |
| 217 | case LM4549_Powerdown_Ctrl_Stat: |
| 218 | value &= ~0xf; |
| 219 | value |= regfile[LM4549_Powerdown_Ctrl_Stat] & 0xf; |
| 220 | regfile[LM4549_Powerdown_Ctrl_Stat] = value; |
| 221 | break; |
| 222 | |
| 223 | case LM4549_Ext_Audio_ID: |
| 224 | case LM4549_Vendor_ID1: |
| 225 | case LM4549_Vendor_ID2: |
| 226 | DPRINTF("Write to read-only register 0x%x\n", (int)offset); |
| 227 | break; |
| 228 | |
| 229 | default: |
| 230 | /* Store the new value */ |
| 231 | regfile[offset] = value; |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | uint32_t lm4549_write_samples(lm4549_state *s, uint32_t left, uint32_t right) |
| 237 | { |
| 238 | /* The left and right samples are in 20-bit resolution. |
| 239 | The LM4549 has 18-bit resolution and only uses the bits [19:2]. |
| 240 | This model supports 16-bit playback. |
| 241 | */ |
| 242 | |
| 243 | if (s->buffer_level > LM4549_BUFFER_SIZE - 2) { |
| 244 | DPRINTF("write_sample Buffer full\n"); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /* Store 16-bit samples in the buffer */ |
| 249 | s->buffer[s->buffer_level++] = (left >> 4); |
| 250 | s->buffer[s->buffer_level++] = (right >> 4); |
| 251 | |
| 252 | if (s->buffer_level == LM4549_BUFFER_SIZE) { |
| 253 | /* Trigger the transfer of the buffer to the audio host */ |
| 254 | lm4549_audio_transfer(s); |
| 255 | } |
| 256 | |
| 257 | return 1; |
| 258 | } |
| 259 | |
| 260 | static int lm4549_post_load(void *opaque, int version_id) |
| 261 | { |
| 262 | lm4549_state *s = (lm4549_state *)opaque; |
| 263 | uint16_t *regfile = s->regfile; |
| 264 | |
| 265 | /* Re-open a voice with the current sample rate */ |
| 266 | uint32_t freq = regfile[LM4549_PCM_Front_DAC_Rate]; |
| 267 | |
| 268 | DPRINTF("post_load freq = %i\n", freq); |
| 269 | DPRINTF("post_load voice_is_active = %i\n", s->voice_is_active); |
| 270 | |
| 271 | struct audsettings as; |
| 272 | as.freq = freq; |
| 273 | as.nchannels = 2; |
| 274 | as.fmt = AUDIO_FORMAT_S16; |
| 275 | as.big_endian = false; |
| 276 | |
| 277 | s->voice = audio_be_open_out( |
| 278 | s->audio_be, |
| 279 | s->voice, |
| 280 | "lm4549.out", |
| 281 | s, |
| 282 | lm4549_audio_out_callback, |
| 283 | &as |
| 284 | ); |
| 285 | |
| 286 | /* Request data */ |
| 287 | if (s->voice_is_active == 1) { |
| 288 | lm4549_audio_out_callback(s, audio_be_get_buffer_size_out(s->audio_be, s->voice)); |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | void lm4549_init(lm4549_state *s, lm4549_callback data_req_cb, void* opaque, |
| 295 | Error **errp) |
| 296 | { |
| 297 | struct audsettings as; |
| 298 | |
| 299 | /* Register an audio card */ |
| 300 | if (!audio_be_check(&s->audio_be, errp)) { |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | /* Store the callback and opaque pointer */ |
| 305 | s->data_req_cb = data_req_cb; |
| 306 | s->opaque = opaque; |
| 307 | |
| 308 | /* Init the registers */ |
| 309 | lm4549_reset(s); |
| 310 | |
| 311 | /* Open a default voice */ |
| 312 | as.freq = 48000; |
| 313 | as.nchannels = 2; |
| 314 | as.fmt = AUDIO_FORMAT_S16; |
| 315 | as.big_endian = false; |
| 316 | |
| 317 | s->voice = audio_be_open_out( |
| 318 | s->audio_be, |
| 319 | s->voice, |
| 320 | "lm4549.out", |
| 321 | s, |
| 322 | lm4549_audio_out_callback, |
| 323 | &as |
| 324 | ); |
| 325 | |
| 326 | audio_be_set_volume_out_lr(s->audio_be, s->voice, 0, 255, 255); |
| 327 | |
| 328 | s->voice_is_active = 0; |
| 329 | |
| 330 | /* Reset the input buffer */ |
| 331 | memset(s->buffer, 0x00, sizeof(s->buffer)); |
| 332 | s->buffer_level = 0; |
| 333 | |
| 334 | #if defined(LM4549_DUMP_DAC_INPUT) |
| 335 | fp_dac_input = fopen("lm4549_dac_input.pcm", "wb"); |
| 336 | if (!fp_dac_input) { |
| 337 | hw_error("Unable to open lm4549_dac_input.pcm for writing\n"); |
| 338 | } |
| 339 | #endif |
| 340 | } |
| 341 | |
| 342 | const VMStateDescription vmstate_lm4549_state = { |
| 343 | .name = "lm4549_state", |
| 344 | .version_id = 1, |
| 345 | .minimum_version_id = 1, |
| 346 | .post_load = lm4549_post_load, |
| 347 | .fields = (const VMStateField[]) { |
| 348 | VMSTATE_UINT32(voice_is_active, lm4549_state), |
| 349 | VMSTATE_UINT16_ARRAY(regfile, lm4549_state, 128), |
| 350 | VMSTATE_UINT16_ARRAY(buffer, lm4549_state, LM4549_BUFFER_SIZE), |
| 351 | VMSTATE_UINT32(buffer_level, lm4549_state), |
| 352 | VMSTATE_END_OF_LIST() |
| 353 | } |
| 354 | }; |