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