2
0

lm4549.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #include "qemu/osdep.h"
  16. #include "hw/hw.h"
  17. #include "audio/audio.h"
  18. #include "lm4549.h"
  19. #include "migration/vmstate.h"
  20. #if 0
  21. #define LM4549_DEBUG 1
  22. #endif
  23. #if 0
  24. #define LM4549_DUMP_DAC_INPUT 1
  25. #endif
  26. #ifdef LM4549_DEBUG
  27. #define DPRINTF(fmt, ...) \
  28. do { printf("lm4549: " fmt , ## __VA_ARGS__); } while (0)
  29. #else
  30. #define DPRINTF(fmt, ...) do {} while (0)
  31. #endif
  32. #if defined(LM4549_DUMP_DAC_INPUT)
  33. static FILE *fp_dac_input;
  34. #endif
  35. /* LM4549 register list */
  36. enum {
  37. LM4549_Reset = 0x00,
  38. LM4549_Master_Volume = 0x02,
  39. LM4549_Line_Out_Volume = 0x04,
  40. LM4549_Master_Volume_Mono = 0x06,
  41. LM4549_PC_Beep_Volume = 0x0A,
  42. LM4549_Phone_Volume = 0x0C,
  43. LM4549_Mic_Volume = 0x0E,
  44. LM4549_Line_In_Volume = 0x10,
  45. LM4549_CD_Volume = 0x12,
  46. LM4549_Video_Volume = 0x14,
  47. LM4549_Aux_Volume = 0x16,
  48. LM4549_PCM_Out_Volume = 0x18,
  49. LM4549_Record_Select = 0x1A,
  50. LM4549_Record_Gain = 0x1C,
  51. LM4549_General_Purpose = 0x20,
  52. LM4549_3D_Control = 0x22,
  53. LM4549_Powerdown_Ctrl_Stat = 0x26,
  54. LM4549_Ext_Audio_ID = 0x28,
  55. LM4549_Ext_Audio_Stat_Ctrl = 0x2A,
  56. LM4549_PCM_Front_DAC_Rate = 0x2C,
  57. LM4549_PCM_ADC_Rate = 0x32,
  58. LM4549_Vendor_ID1 = 0x7C,
  59. LM4549_Vendor_ID2 = 0x7E
  60. };
  61. static void lm4549_reset(lm4549_state *s)
  62. {
  63. uint16_t *regfile = s->regfile;
  64. regfile[LM4549_Reset] = 0x0d50;
  65. regfile[LM4549_Master_Volume] = 0x8008;
  66. regfile[LM4549_Line_Out_Volume] = 0x8000;
  67. regfile[LM4549_Master_Volume_Mono] = 0x8000;
  68. regfile[LM4549_PC_Beep_Volume] = 0x0000;
  69. regfile[LM4549_Phone_Volume] = 0x8008;
  70. regfile[LM4549_Mic_Volume] = 0x8008;
  71. regfile[LM4549_Line_In_Volume] = 0x8808;
  72. regfile[LM4549_CD_Volume] = 0x8808;
  73. regfile[LM4549_Video_Volume] = 0x8808;
  74. regfile[LM4549_Aux_Volume] = 0x8808;
  75. regfile[LM4549_PCM_Out_Volume] = 0x8808;
  76. regfile[LM4549_Record_Select] = 0x0000;
  77. regfile[LM4549_Record_Gain] = 0x8000;
  78. regfile[LM4549_General_Purpose] = 0x0000;
  79. regfile[LM4549_3D_Control] = 0x0101;
  80. regfile[LM4549_Powerdown_Ctrl_Stat] = 0x000f;
  81. regfile[LM4549_Ext_Audio_ID] = 0x0001;
  82. regfile[LM4549_Ext_Audio_Stat_Ctrl] = 0x0000;
  83. regfile[LM4549_PCM_Front_DAC_Rate] = 0xbb80;
  84. regfile[LM4549_PCM_ADC_Rate] = 0xbb80;
  85. regfile[LM4549_Vendor_ID1] = 0x4e53;
  86. regfile[LM4549_Vendor_ID2] = 0x4331;
  87. }
  88. static void lm4549_audio_transfer(lm4549_state *s)
  89. {
  90. uint32_t written_bytes, written_samples;
  91. uint32_t i;
  92. /* Activate the voice */
  93. AUD_set_active_out(s->voice, 1);
  94. s->voice_is_active = 1;
  95. /* Try to write the buffer content */
  96. written_bytes = AUD_write(s->voice, s->buffer,
  97. s->buffer_level * sizeof(uint16_t));
  98. written_samples = written_bytes >> 1;
  99. #if defined(LM4549_DUMP_DAC_INPUT)
  100. fwrite(s->buffer, sizeof(uint8_t), written_bytes, fp_dac_input);
  101. #endif
  102. s->buffer_level -= written_samples;
  103. if (s->buffer_level > 0) {
  104. /* Move the data back to the start of the buffer */
  105. for (i = 0; i < s->buffer_level; i++) {
  106. s->buffer[i] = s->buffer[i + written_samples];
  107. }
  108. }
  109. }
  110. static void lm4549_audio_out_callback(void *opaque, int free)
  111. {
  112. lm4549_state *s = (lm4549_state *)opaque;
  113. static uint32_t prev_buffer_level;
  114. #ifdef LM4549_DEBUG
  115. int size = AUD_get_buffer_size_out(s->voice);
  116. DPRINTF("audio_out_callback size = %i free = %i\n", size, free);
  117. #endif
  118. /* Detect that no data are consumed
  119. => disable the voice */
  120. if (s->buffer_level == prev_buffer_level) {
  121. AUD_set_active_out(s->voice, 0);
  122. s->voice_is_active = 0;
  123. }
  124. prev_buffer_level = s->buffer_level;
  125. /* Check if a buffer transfer is pending */
  126. if (s->buffer_level == LM4549_BUFFER_SIZE) {
  127. lm4549_audio_transfer(s);
  128. /* Request more data */
  129. if (s->data_req_cb != NULL) {
  130. (s->data_req_cb)(s->opaque);
  131. }
  132. }
  133. }
  134. uint32_t lm4549_read(lm4549_state *s, hwaddr offset)
  135. {
  136. uint16_t *regfile = s->regfile;
  137. uint32_t value = 0;
  138. /* Read the stored value */
  139. assert(offset < 128);
  140. value = regfile[offset];
  141. DPRINTF("read [0x%02x] = 0x%04x\n", offset, value);
  142. return value;
  143. }
  144. void lm4549_write(lm4549_state *s,
  145. hwaddr offset, uint32_t value)
  146. {
  147. uint16_t *regfile = s->regfile;
  148. assert(offset < 128);
  149. DPRINTF("write [0x%02x] = 0x%04x\n", offset, value);
  150. switch (offset) {
  151. case LM4549_Reset:
  152. lm4549_reset(s);
  153. break;
  154. case LM4549_PCM_Front_DAC_Rate:
  155. regfile[LM4549_PCM_Front_DAC_Rate] = value;
  156. DPRINTF("DAC rate change = %i\n", value);
  157. /* Re-open a voice with the new sample rate */
  158. struct audsettings as;
  159. as.freq = value;
  160. as.nchannels = 2;
  161. as.fmt = AUDIO_FORMAT_S16;
  162. as.endianness = 0;
  163. s->voice = AUD_open_out(
  164. &s->card,
  165. s->voice,
  166. "lm4549.out",
  167. s,
  168. lm4549_audio_out_callback,
  169. &as
  170. );
  171. break;
  172. case LM4549_Powerdown_Ctrl_Stat:
  173. value &= ~0xf;
  174. value |= regfile[LM4549_Powerdown_Ctrl_Stat] & 0xf;
  175. regfile[LM4549_Powerdown_Ctrl_Stat] = value;
  176. break;
  177. case LM4549_Ext_Audio_ID:
  178. case LM4549_Vendor_ID1:
  179. case LM4549_Vendor_ID2:
  180. DPRINTF("Write to read-only register 0x%x\n", (int)offset);
  181. break;
  182. default:
  183. /* Store the new value */
  184. regfile[offset] = value;
  185. break;
  186. }
  187. }
  188. uint32_t lm4549_write_samples(lm4549_state *s, uint32_t left, uint32_t right)
  189. {
  190. /* The left and right samples are in 20-bit resolution.
  191. The LM4549 has 18-bit resolution and only uses the bits [19:2].
  192. This model supports 16-bit playback.
  193. */
  194. if (s->buffer_level > LM4549_BUFFER_SIZE - 2) {
  195. DPRINTF("write_sample Buffer full\n");
  196. return 0;
  197. }
  198. /* Store 16-bit samples in the buffer */
  199. s->buffer[s->buffer_level++] = (left >> 4);
  200. s->buffer[s->buffer_level++] = (right >> 4);
  201. if (s->buffer_level == LM4549_BUFFER_SIZE) {
  202. /* Trigger the transfer of the buffer to the audio host */
  203. lm4549_audio_transfer(s);
  204. }
  205. return 1;
  206. }
  207. static int lm4549_post_load(void *opaque, int version_id)
  208. {
  209. lm4549_state *s = (lm4549_state *)opaque;
  210. uint16_t *regfile = s->regfile;
  211. /* Re-open a voice with the current sample rate */
  212. uint32_t freq = regfile[LM4549_PCM_Front_DAC_Rate];
  213. DPRINTF("post_load freq = %i\n", freq);
  214. DPRINTF("post_load voice_is_active = %i\n", s->voice_is_active);
  215. struct audsettings as;
  216. as.freq = freq;
  217. as.nchannels = 2;
  218. as.fmt = AUDIO_FORMAT_S16;
  219. as.endianness = 0;
  220. s->voice = AUD_open_out(
  221. &s->card,
  222. s->voice,
  223. "lm4549.out",
  224. s,
  225. lm4549_audio_out_callback,
  226. &as
  227. );
  228. /* Request data */
  229. if (s->voice_is_active == 1) {
  230. lm4549_audio_out_callback(s, AUD_get_buffer_size_out(s->voice));
  231. }
  232. return 0;
  233. }
  234. void lm4549_init(lm4549_state *s, lm4549_callback data_req_cb, void* opaque)
  235. {
  236. struct audsettings as;
  237. /* Store the callback and opaque pointer */
  238. s->data_req_cb = data_req_cb;
  239. s->opaque = opaque;
  240. /* Init the registers */
  241. lm4549_reset(s);
  242. /* Register an audio card */
  243. AUD_register_card("lm4549", &s->card);
  244. /* Open a default voice */
  245. as.freq = 48000;
  246. as.nchannels = 2;
  247. as.fmt = AUDIO_FORMAT_S16;
  248. as.endianness = 0;
  249. s->voice = AUD_open_out(
  250. &s->card,
  251. s->voice,
  252. "lm4549.out",
  253. s,
  254. lm4549_audio_out_callback,
  255. &as
  256. );
  257. AUD_set_volume_out(s->voice, 0, 255, 255);
  258. s->voice_is_active = 0;
  259. /* Reset the input buffer */
  260. memset(s->buffer, 0x00, sizeof(s->buffer));
  261. s->buffer_level = 0;
  262. #if defined(LM4549_DUMP_DAC_INPUT)
  263. fp_dac_input = fopen("lm4549_dac_input.pcm", "wb");
  264. if (!fp_dac_input) {
  265. hw_error("Unable to open lm4549_dac_input.pcm for writing\n");
  266. }
  267. #endif
  268. }
  269. const VMStateDescription vmstate_lm4549_state = {
  270. .name = "lm4549_state",
  271. .version_id = 1,
  272. .minimum_version_id = 1,
  273. .post_load = lm4549_post_load,
  274. .fields = (VMStateField[]) {
  275. VMSTATE_UINT32(voice_is_active, lm4549_state),
  276. VMSTATE_UINT16_ARRAY(regfile, lm4549_state, 128),
  277. VMSTATE_UINT16_ARRAY(buffer, lm4549_state, LM4549_BUFFER_SIZE),
  278. VMSTATE_UINT32(buffer_level, lm4549_state),
  279. VMSTATE_END_OF_LIST()
  280. }
  281. };