luat_audio_es8311.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. #include "luat_base.h"
  2. #include "luat_gpio.h"
  3. #include "luat_i2c.h"
  4. #include "luat_audio.h"
  5. #include "luat_rtos.h"
  6. #define LUAT_LOG_TAG "es8311"
  7. #include "luat_log.h"
  8. #define ES8311_ADDR 0x18
  9. #define MCLK_DIV_FRE 256
  10. #define ES8311_MCLK_SOURCE 0 //是否硬件没接MCLK需要用SCLK当作MCLK,一般不用,除非引脚非常不够要省引脚
  11. #define ES8311_DMIC_SEL 0 //DMIC选择:默认选择关闭0,打开为1
  12. #define ES8311_LINSEL_SEL 1 //0 – no input selection 1 – select Mic1p-Mic1n 2 – select Mic2p-Mic2n 3 – select both pairs of Mic1 and Mic2
  13. #define ES8311_ADC_PGA_GAIN 8 //ADC模拟增益:(选择范围0~10),具体对应关系见相应DS说明
  14. #define ES8311_ADC2DAC_SEL 0 //LOOP选择:内部ADC数据给到DAC自回环输出:默认选择关闭0,打开为1
  15. #define ES8311_DAC_HP_ON 0 //输出负载开启HP驱动:默认选择关闭0,打开为1
  16. #define ES8311_VDDA_3V3 0x00
  17. #define ES8311_VDDA_1V8 0x01
  18. #define ES8311_VDDA_VOLTAGE ES8311_VDDA_3V3 //模拟电压选择为3V3还是1V8,需要和实际硬件匹配,默认选择3V3
  19. /* ES8311_REGISTER NAME_REG_REGISTER ADDRESS */
  20. #define ES8311_RESET_REG00 0x00 /*reset digital,csm,clock manager etc.*/
  21. /* Clock Scheme Register definition */
  22. #define ES8311_CLK_MANAGER_REG01 0x01 /* select clk src for mclk, enable clock for codec */
  23. #define ES8311_CLK_MANAGER_REG02 0x02 /* clk divider and clk multiplier */
  24. #define ES8311_CLK_MANAGER_REG03 0x03 /* adc fsmode and osr */
  25. #define ES8311_CLK_MANAGER_REG04 0x04 /* dac osr */
  26. #define ES8311_CLK_MANAGER_REG05 0x05 /* clk divier for adc and dac */
  27. #define ES8311_CLK_MANAGER_REG06 0x06 /* bclk inverter and divider */
  28. #define ES8311_CLK_MANAGER_REG07 0x07 /* tri-state, lrck divider */
  29. #define ES8311_CLK_MANAGER_REG08 0x08 /* lrck divider */
  30. #define ES8311_SDPIN_REG09 0x09 /* dac serial digital port */
  31. #define ES8311_SDPIN_REG09_DACWL_MASK (7 << 2)
  32. #define ES8311_SDPIN_REG09_DACWL_SHIFT 2
  33. #define ES8311_SDPOUT_REG0A 0x0A /* adc serial digital port */
  34. #define ES8311_SDPOUT_REG0A_ADCWL_MASK (7 << 2)
  35. #define ES8311_SDPOUT_REG0A_ADCWL_SHIFT 2
  36. #define ES8311_SYSTEM_REG0B 0x0B /* system */
  37. #define ES8311_SYSTEM_REG0C 0x0C /* system */
  38. #define ES8311_SYSTEM_REG0D 0x0D /* system, power up/down */
  39. #define ES8311_SYSTEM_REG0E 0x0E /* system, power up/down */
  40. #define ES8311_SYSTEM_REG0F 0x0F /* system, low power */
  41. #define ES8311_SYSTEM_REG10 0x10 /* system */
  42. #define ES8311_SYSTEM_REG11 0x11 /* system */
  43. #define ES8311_SYSTEM_REG12 0x12 /* system, Enable DAC */
  44. #define ES8311_SYSTEM_REG13 0x13 /* system */
  45. #define ES8311_SYSTEM_REG14 0x14 /* system, select DMIC, select analog pga gain */
  46. #define ES8311_ADC_REG15 0x15 /* ADC, adc ramp rate, dmic sense */
  47. #define ES8311_ADC_REG16 0x16 /* ADC */
  48. #define ES8311_ADC_REG17 0x17 /* ADC, volume */
  49. #define ES8311_ADC_REG18 0x18 /* ADC, alc enable and winsize */
  50. #define ES8311_ADC_REG19 0x19 /* ADC, alc maxlevel */
  51. #define ES8311_ADC_REG1A 0x1A /* ADC, alc automute */
  52. #define ES8311_ADC_REG1B 0x1B /* ADC, alc automute, adc hpf s1 */
  53. #define ES8311_ADC_REG1C 0x1C /* ADC, equalizer, hpf s2 */
  54. #define ES8311_DAC_REG31 0x31 /* DAC, mute */
  55. #define ES8311_DAC_REG32 0x32 /* DAC, volume */
  56. #define ES8311_DAC_REG33 0x33 /* DAC, offset */
  57. #define ES8311_DAC_REG34 0x34 /* DAC, drc enable, drc winsize */
  58. #define ES8311_DAC_REG35 0x35 /* DAC, drc maxlevel, minilevel */
  59. #define ES8311_DAC_REG37 0x37 /* DAC, ramprate */
  60. #define ES8311_GPIO_REG44 0x44 /* GPIO, dac2adc for test */
  61. #define ES8311_GP_REG45 0x45 /* GP CONTROL */
  62. #define ES8311_CHD1_REGFD 0xFD /* CHIP ID1 */
  63. #define ES8311_CHD2_REGFE 0xFE /* CHIP ID2 */
  64. #define ES8311_CHVER_REGFF 0xFF /* VERSION */
  65. #define ES8311_CHD1_REGFD 0xFD /* CHIP ID1 */
  66. #define ES8311_MAX_REGISTER 0xFF
  67. //static uint8_t es8311_dacvol_bak=0,es8311_adcvol_bak=0;
  68. typedef struct{
  69. uint32_t mclk; // mclk frequency
  70. uint32_t rate; // sample rate
  71. uint8_t preDiv; // the pre divider with range from 1 to 8
  72. uint8_t preMulti; // the pre multiplier with x1, x2, x4 and x8 selection
  73. uint8_t adcDiv; // adcclk divider
  74. uint8_t dacDiv; // dacclk divider
  75. uint8_t fsMode; // double speed or single speed, =0, ss, =1, ds
  76. uint8_t lrckH; // adclrck divider and daclrck divider
  77. uint8_t lrckL;
  78. uint8_t bclkDiv; // sclk divider
  79. uint8_t adcOsr; // adc osr
  80. uint8_t dacOsr; // dac osr
  81. }es8311_codec_div_t;
  82. // codec hifi mclk clock divider coefficients
  83. static const es8311_codec_div_t es8311_codec_div[] =
  84. {
  85. //mclk rate preDiv mult adcDiv dacDiv fsMode lrch lrcl bckdiv adcOsr dacOsr
  86. // 8k
  87. {12288000, 8000 , 0x06, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  88. {18432000, 8000 , 0x03, 0x02, 0x03, 0x03, 0x00, 0x05, 0xff, 0x18, 0x10, 0x20},
  89. {16384000, 8000 , 0x08, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  90. {8192000 , 8000 , 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  91. {6144000 , 8000 , 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  92. {4096000 , 8000 , 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  93. {3072000 , 8000 , 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  94. {2048000 , 8000 , 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  95. {1536000 , 8000 , 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  96. {1024000 , 8000 , 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  97. // 16k
  98. {12288000, 16000, 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  99. {18432000, 16000, 0x03, 0x02, 0x03, 0x03, 0x00, 0x02, 0xff, 0x0c, 0x10, 0x20},
  100. {16384000, 16000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  101. {8192000 , 16000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  102. {6144000 , 16000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  103. {4096000 , 16000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  104. {3072000 , 16000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  105. {2048000 , 16000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  106. {1536000 , 16000, 0x03, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  107. {1024000 , 16000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x20},
  108. // 22.05k
  109. {11289600, 22050, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  110. {5644800 , 22050, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  111. {2822400 , 22050, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  112. {1411200 , 22050, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  113. // 32k
  114. {12288000, 32000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  115. {18432000, 32000, 0x03, 0x04, 0x03, 0x03, 0x00, 0x02, 0xff, 0x0c, 0x10, 0x10},
  116. {16384000, 32000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  117. {8192000 , 32000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  118. {6144000 , 32000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  119. {4096000 , 32000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  120. {3072000 , 32000, 0x03, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  121. {2048000 , 32000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  122. {1536000 , 32000, 0x03, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
  123. {1024000 , 32000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  124. // 44.1k
  125. {11289600, 44100, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  126. {5644800 , 44100, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  127. {2822400 , 44100, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  128. {1411200 , 44100, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  129. // 48k
  130. {12288000, 48000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  131. {18432000, 48000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  132. {6144000 , 48000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  133. {3072000 , 48000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  134. {1536000 , 48000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  135. // 96k
  136. {12288000, 96000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  137. {18432000, 96000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  138. {6144000 , 96000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  139. {3072000 , 96000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
  140. {1536000 , 96000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
  141. };
  142. static int es8311_get_coeff(uint32_t mclk, uint32_t rate){
  143. for (int i = 0; i < (sizeof(es8311_codec_div) / sizeof(es8311_codec_div[0])); i++) {
  144. if (es8311_codec_div[i].rate == rate && es8311_codec_div[i].mclk == mclk)
  145. return i;
  146. }
  147. return -1;
  148. }
  149. static void es8311_write_reg(luat_audio_codec_conf_t* conf,uint8_t addr, uint8_t data){
  150. uint8_t temp[] = {addr,data};
  151. luat_i2c_send(conf->i2c_id, ES8311_ADDR, temp, 2 , 1);
  152. luat_rtos_task_sleep(1);
  153. }
  154. static uint8_t es8311_read_reg(luat_audio_codec_conf_t* conf,uint8_t addr){
  155. uint8_t temp=0;
  156. luat_i2c_send(conf->i2c_id, ES8311_ADDR, &addr, 1 , 0);
  157. luat_i2c_recv(conf->i2c_id, ES8311_ADDR, &temp, 1);
  158. return temp;
  159. }
  160. static int es8311_mode_resume(luat_audio_codec_conf_t* conf,uint8_t selece){
  161. es8311_write_reg(conf,ES8311_SYSTEM_REG0D,0x01);
  162. if (selece == LUAT_CODEC_MODE_ALL)
  163. es8311_write_reg(conf,ES8311_GP_REG45,0x00);
  164. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x3F + (ES8311_MCLK_SOURCE<<7));
  165. es8311_write_reg(conf,ES8311_RESET_REG00,0x80);
  166. luat_rtos_task_sleep(1);
  167. es8311_write_reg(conf,ES8311_SYSTEM_REG0D,0x01);
  168. if (selece == LUAT_CODEC_MODE_ALL)
  169. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG02,0x00);
  170. es8311_write_reg(conf,ES8311_DAC_REG37,0x08);
  171. if (selece == LUAT_CODEC_MODE_ALL)
  172. es8311_write_reg(conf,ES8311_ADC_REG15,0x40);
  173. else
  174. es8311_write_reg(conf,ES8311_ADC_REG15,0x00);
  175. if(selece != LUAT_CODEC_MODE_ADC)
  176. es8311_write_reg(conf,ES8311_SYSTEM_REG12,0x00);
  177. if(selece != LUAT_CODEC_MODE_DAC)
  178. es8311_write_reg(conf,ES8311_SYSTEM_REG14,(ES8311_DMIC_SEL<<6) + (ES8311_LINSEL_SEL<<4) + ES8311_ADC_PGA_GAIN);
  179. if (selece == LUAT_CODEC_MODE_ALL)
  180. es8311_write_reg(conf,ES8311_SYSTEM_REG0E,0x00);
  181. else
  182. es8311_write_reg(conf,ES8311_SYSTEM_REG0E,0x02);
  183. // if(selece != LUAT_CODEC_MODE_DAC)
  184. // es8311_write_reg(conf,ES8311_ADC_REG17,es8311_adcvol_bak);
  185. // if(selece != LUAT_CODEC_MODE_ADC)
  186. // es8311_write_reg(conf,ES8311_DAC_REG32,es8311_dacvol_bak);
  187. // luat_rtos_task_sleep(50);
  188. if(selece == LUAT_CODEC_MODE_ADC)
  189. es8311_write_reg(conf,ES8311_SDPOUT_REG0A,0x00);
  190. return 0;
  191. }
  192. static int es8311_mode_standby(luat_audio_codec_conf_t* conf,uint8_t selece){
  193. if(selece != LUAT_CODEC_MODE_ADC){
  194. //es8311_dacvol_bak = es8311_read_reg(conf,ES8311_DAC_REG32);
  195. es8311_write_reg(conf,ES8311_DAC_REG32,0x00);
  196. }
  197. if(selece != LUAT_CODEC_MODE_DAC){
  198. //es8311_adcvol_bak = es8311_read_reg(conf,ES8311_ADC_REG17);
  199. es8311_write_reg(conf,ES8311_ADC_REG17,0x00);
  200. }
  201. if(selece == LUAT_CODEC_MODE_ADC){
  202. es8311_write_reg(conf,ES8311_SDPOUT_REG0A,0x40);
  203. es8311_write_reg(conf,ES8311_SYSTEM_REG0E,0x7f);
  204. es8311_write_reg(conf,ES8311_SYSTEM_REG14,0x00);
  205. es8311_write_reg(conf,ES8311_SYSTEM_REG0D,0x31);
  206. }else if(selece == LUAT_CODEC_MODE_DAC){
  207. es8311_write_reg(conf,ES8311_SYSTEM_REG0E,0x0F);
  208. es8311_write_reg(conf,ES8311_SYSTEM_REG12,0x02);
  209. es8311_write_reg(conf,ES8311_SYSTEM_REG0D,0x09);
  210. }else{
  211. es8311_write_reg(conf,ES8311_SYSTEM_REG0E,0xFF);
  212. es8311_write_reg(conf,ES8311_SYSTEM_REG12,0x02);
  213. es8311_write_reg(conf,ES8311_SYSTEM_REG14,0x00);
  214. es8311_write_reg(conf,ES8311_SYSTEM_REG0D, 0xFA);//0xF9
  215. }
  216. es8311_write_reg(conf,ES8311_ADC_REG15, 0x00);
  217. es8311_write_reg(conf,ES8311_DAC_REG37, 0x08);
  218. if(selece == LUAT_CODEC_MODE_ADC){
  219. es8311_write_reg(conf,ES8311_RESET_REG00,0x82);
  220. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x35);
  221. }else if(selece == LUAT_CODEC_MODE_DAC){
  222. es8311_write_reg(conf,ES8311_RESET_REG00, 0x81);
  223. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x3a);
  224. }else{
  225. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG02,0x10);
  226. es8311_write_reg(conf,ES8311_RESET_REG00,0x00);
  227. es8311_write_reg(conf,ES8311_RESET_REG00,0x1F);
  228. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x30);
  229. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x00);
  230. es8311_write_reg(conf,ES8311_GP_REG45,0x01);
  231. }
  232. return 0;
  233. }
  234. static int es8311_mode_pwrdown(luat_audio_codec_conf_t* conf){
  235. luat_audio_play_blank(conf->multimedia_id, 1);
  236. //es8311_dacvol_bak = es8311_read_reg(conf,ES8311_DAC_REG32);
  237. //es8311_adcvol_bak = es8311_read_reg(conf,ES8311_ADC_REG17);
  238. es8311_write_reg(conf,ES8311_DAC_REG32,0x00);
  239. es8311_write_reg(conf,ES8311_ADC_REG17,0x00);
  240. es8311_write_reg(conf,ES8311_SYSTEM_REG0E,0xff);
  241. es8311_write_reg(conf,ES8311_SYSTEM_REG12,0x02);
  242. es8311_write_reg(conf,ES8311_SYSTEM_REG14,0x00);
  243. es8311_write_reg(conf,ES8311_SYSTEM_REG0D,0xf9);
  244. es8311_write_reg(conf,ES8311_ADC_REG15,0x00);
  245. es8311_write_reg(conf,ES8311_DAC_REG37,0x08);
  246. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG02,0x10);
  247. es8311_write_reg(conf,ES8311_RESET_REG00,0x00);
  248. es8311_write_reg(conf,ES8311_RESET_REG00,0x1f);
  249. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x30);
  250. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x00);
  251. es8311_write_reg(conf,ES8311_GP_REG45,0x00);
  252. es8311_write_reg(conf,ES8311_SYSTEM_REG0D,0xfc);
  253. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG02,0x00);
  254. luat_rtos_task_sleep(50);
  255. luat_audio_play_blank(conf->multimedia_id, 0);
  256. return 0;
  257. }
  258. // mute
  259. static uint8_t es8311_set_mute(luat_audio_codec_conf_t* conf,uint8_t enable){
  260. es8311_write_reg(conf,ES8311_DAC_REG31, enable?0x20:0x00);
  261. return 0;
  262. }
  263. static uint8_t es8311_get_mute(luat_audio_codec_conf_t* conf){
  264. uint8_t reg = es8311_read_reg(conf,ES8311_DAC_REG31);
  265. return (reg & 0x20) >> 5;
  266. }
  267. // voice_vol
  268. static uint8_t es8311_set_voice_vol(luat_audio_codec_conf_t* conf,uint8_t vol){
  269. if(vol > 100) return -1;
  270. es8311_write_reg(conf,ES8311_DAC_REG32,(uint8_t)(vol * 2550 / 1000));
  271. return 0;
  272. }
  273. static uint8_t es8311_get_voice_vol(luat_audio_codec_conf_t* conf){
  274. uint8_t reg = es8311_read_reg(conf,ES8311_DAC_REG32);
  275. return reg * 1000 / 2550;
  276. }
  277. // mic_vol
  278. static uint8_t es8311_set_mic_vol(luat_audio_codec_conf_t* conf,uint8_t vol){
  279. if(vol > 100) return -1;
  280. es8311_write_reg(conf,ES8311_ADC_REG17,(uint8_t)(vol * 2550 / 1000));
  281. return 0;
  282. }
  283. static uint8_t es8311_get_mic_vol(luat_audio_codec_conf_t* conf){
  284. uint8_t reg = es8311_read_reg(conf,ES8311_ADC_REG17);
  285. return reg * 1000 / 2550;
  286. }
  287. static int es8311_codec_samplerate(luat_audio_codec_conf_t* conf,uint16_t samplerate){
  288. if(samplerate != 8000 && samplerate != 16000 && samplerate != 32000 &&
  289. samplerate != 11025 && samplerate != 22050 && samplerate != 44100 &&
  290. samplerate != 12000 && samplerate != 24000 && samplerate != 48000)
  291. {
  292. LLOGE("samplerate error! samplerate:%d\n",samplerate);
  293. return -1;
  294. }
  295. int coeff = es8311_get_coeff(samplerate * MCLK_DIV_FRE, samplerate);
  296. if (coeff < 0){
  297. LLOGE("Unable to configure sample rate %dHz with %dHz MCLK", samplerate, samplerate * MCLK_DIV_FRE);
  298. return -1;
  299. }
  300. uint8_t reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG02) & 0x07;
  301. reg |= (es8311_codec_div[coeff].preDiv - 1) << 5;
  302. uint8_t datmp = 0;
  303. switch (es8311_codec_div[coeff].preMulti){
  304. case 1:
  305. datmp = 0;
  306. break;
  307. case 2:
  308. datmp = 1;
  309. break;
  310. case 4:
  311. datmp = 2;
  312. break;
  313. case 8:
  314. datmp = 3;
  315. break;
  316. default:
  317. break;
  318. }
  319. reg |= (datmp) << 3;
  320. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG02, reg);
  321. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG05) & 0x00;
  322. reg |= (es8311_codec_div[coeff].adcDiv - 1) << 4;
  323. reg |= (es8311_codec_div[coeff].dacDiv - 1) << 0;
  324. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG05, reg);
  325. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG03) & 0x80;
  326. reg |= es8311_codec_div[coeff].fsMode << 6;
  327. reg |= es8311_codec_div[coeff].adcOsr << 0;
  328. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG03, reg);
  329. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG04) & 0x80;
  330. reg |= es8311_codec_div[coeff].dacOsr << 0;
  331. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG04, reg);
  332. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG07) & 0xC0;
  333. reg |= es8311_codec_div[coeff].lrckH << 0;
  334. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG07, reg);
  335. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG08) & 0x00;
  336. reg |= es8311_codec_div[coeff].lrckL << 0;
  337. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG08, reg);
  338. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG06) & 0xE0;
  339. if (es8311_codec_div[coeff].bclkDiv < 19)
  340. reg |= (es8311_codec_div[coeff].bclkDiv - 1) << 0;
  341. else
  342. reg |= (es8311_codec_div[coeff].bclkDiv) << 0;
  343. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG06, reg);
  344. return 0;
  345. }
  346. static int es8311_set_format(luat_audio_codec_conf_t* conf,uint8_t format){
  347. // only support I2S format now
  348. uint8_t dacIface = es8311_read_reg(conf,ES8311_SDPIN_REG09);
  349. uint8_t adcIface = es8311_read_reg(conf,ES8311_SDPOUT_REG0A);
  350. dacIface &= 0xFC;
  351. adcIface &= 0xFC;
  352. es8311_write_reg(conf,ES8311_SDPIN_REG09, dacIface);
  353. es8311_write_reg(conf,ES8311_SDPOUT_REG0A, adcIface);
  354. return 0;
  355. }
  356. static int es8311_codec_samplebits(luat_audio_codec_conf_t* conf,uint8_t samplebits){
  357. if(samplebits != 8 && samplebits != 16 && samplebits != 24 && samplebits != 32){
  358. LLOGE("bit_width error!\n");
  359. return -1;
  360. }
  361. int wl;
  362. switch (samplebits){
  363. case 16:
  364. wl = 3;
  365. break;
  366. case 18:
  367. wl = 2;
  368. break;
  369. case 20:
  370. wl = 1;
  371. break;
  372. case 24:
  373. wl = 0;
  374. break;
  375. case 32:
  376. wl = 4;
  377. break;
  378. default:
  379. return -1;
  380. }
  381. uint8_t dac_iface = es8311_read_reg(conf,ES8311_SDPIN_REG09);
  382. uint8_t adc_iface = es8311_read_reg(conf,ES8311_SDPOUT_REG0A);
  383. dac_iface |= wl << ES8311_SDPIN_REG09_DACWL_SHIFT;
  384. adc_iface |= wl << ES8311_SDPOUT_REG0A_ADCWL_SHIFT;
  385. es8311_write_reg(conf,ES8311_SDPIN_REG09, dac_iface);
  386. es8311_write_reg(conf,ES8311_SDPOUT_REG0A, adc_iface);
  387. return 0;
  388. }
  389. static int es8311_codec_channels(luat_audio_codec_conf_t* conf,uint8_t channels){
  390. return 0;
  391. }
  392. static inline void es8311_reset(luat_audio_codec_conf_t* conf){
  393. es8311_write_reg(conf,ES8311_RESET_REG00, 0x1F);
  394. es8311_write_reg(conf,ES8311_RESET_REG00, 0x80);
  395. luat_rtos_task_sleep(1);
  396. es8311_write_reg(conf,ES8311_SYSTEM_REG0D, 0x01);
  397. }
  398. static int es8311_codec_init(luat_audio_codec_conf_t* conf,uint8_t mode){
  399. luat_audio_power(conf->multimedia_id,1);
  400. luat_rtos_task_sleep(50);
  401. luat_audio_conf_t* audio_conf = luat_audio_get_config(conf->multimedia_id);
  402. uint8_t temp1 = es8311_read_reg(conf,ES8311_CHD1_REGFD);
  403. uint8_t temp2 = es8311_read_reg(conf,ES8311_CHD2_REGFE);
  404. uint8_t temp3 = es8311_read_reg(conf,ES8311_CHVER_REGFF);
  405. if(temp1 != 0x83 || temp2 != 0x11){
  406. LLOGE("codec err, id = 0x%x 0x%x ver = 0x%x", temp1, temp2, temp3);
  407. return -1;
  408. }
  409. /* reset codec */
  410. es8311_reset(conf);
  411. // BCLK/LRCK pullup on
  412. es8311_write_reg(conf,ES8311_GP_REG45, 0x00);
  413. /* power up digital */
  414. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01, 0x30);
  415. // SET SDP in and SDP out mute
  416. es8311_write_reg(conf,ES8311_SDPIN_REG09, (es8311_read_reg(conf,ES8311_SDPIN_REG09) & 0xBF));
  417. es8311_write_reg(conf,ES8311_SDPOUT_REG0A, (es8311_read_reg(conf,ES8311_SDPOUT_REG0A) & 0xBF));
  418. es8311_write_reg(conf,ES8311_SYSTEM_REG0B, 0x00);
  419. es8311_write_reg(conf,ES8311_SYSTEM_REG0C, 0x00);
  420. if (audio_conf)
  421. es8311_write_reg(conf,ES8311_SYSTEM_REG10, (0x1C*ES8311_DAC_HP_ON) + (0x60 * (audio_conf->voltage ? ES8311_VDDA_1V8 : ES8311_VDDA_3V3)) + 0x03);
  422. else
  423. es8311_write_reg(conf,ES8311_SYSTEM_REG10, (0x1C*ES8311_DAC_HP_ON) + (0x60 * ES8311_VDDA_VOLTAGE) + 0x03);
  424. es8311_write_reg(conf,ES8311_SYSTEM_REG11, 0x7F);
  425. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x3F + (ES8311_MCLK_SOURCE<<7));
  426. es8311_write_reg(conf,ES8311_RESET_REG00, 0X80+(mode<<6));
  427. luat_rtos_task_sleep(1);
  428. es8311_write_reg(conf,ES8311_SYSTEM_REG0D, 0x01);
  429. es8311_write_reg(conf,ES8311_SYSTEM_REG14,(ES8311_DMIC_SEL<<6) + (ES8311_LINSEL_SEL<<4) + ES8311_ADC_PGA_GAIN);
  430. es8311_write_reg(conf,ES8311_SYSTEM_REG12, 0x28);
  431. es8311_write_reg(conf,ES8311_SYSTEM_REG13, 0x00 + (ES8311_DAC_HP_ON<<4));
  432. /* set normal power mode */
  433. es8311_write_reg(conf,ES8311_SYSTEM_REG0E, 0x02);
  434. es8311_write_reg(conf,ES8311_SYSTEM_REG0F, 0x44);
  435. // start up vmid normal speed charge
  436. es8311_write_reg(conf,ES8311_ADC_REG15, 0x00);
  437. /* set adc hpf */
  438. es8311_write_reg(conf,ES8311_ADC_REG1B, 0x0A);
  439. /* set adc hpf,ADC_EQ bypass */
  440. es8311_write_reg(conf,ES8311_ADC_REG1C, 0x6A);
  441. /* set dac softramp,disable DAC_EQ */
  442. es8311_write_reg(conf,ES8311_DAC_REG37, 0x08);
  443. // set internal reference signal (ADC + DAC)
  444. es8311_write_reg(conf,ES8311_GPIO_REG44, (ES8311_ADC2DAC_SEL<<7));
  445. // adc vol
  446. //es8311_write_reg(conf,ES8311_ADC_REG17, es8311_adcvol_bak);
  447. // dac vol
  448. //es8311_write_reg(conf,ES8311_DAC_REG32, es8311_dacvol_bak);
  449. return 0;
  450. }
  451. static int es8311_codec_deinit(luat_audio_codec_conf_t* conf){
  452. return 0;
  453. }
  454. static int es8311_codec_control(luat_audio_codec_conf_t* conf,luat_audio_codec_ctl_t cmd,uint32_t data){
  455. switch (cmd){
  456. case LUAT_CODEC_MODE_RESUME:
  457. es8311_mode_resume(conf,(uint8_t)data);
  458. break;
  459. case LUAT_CODEC_MODE_STANDBY:
  460. es8311_mode_standby(conf,(uint8_t)data);
  461. break;
  462. case LUAT_CODEC_MODE_PWRDOWN:
  463. es8311_mode_pwrdown(conf);
  464. break;
  465. case LUAT_CODEC_SET_MUTE:
  466. es8311_set_mute(conf,(uint8_t)data);
  467. break;
  468. case LUAT_CODEC_GET_MUTE:
  469. return es8311_get_mute(conf);
  470. break;
  471. case LUAT_CODEC_SET_VOICE_VOL:
  472. es8311_set_voice_vol(conf,(uint8_t)data);
  473. break;
  474. case LUAT_CODEC_GET_VOICE_VOL:
  475. return es8311_get_voice_vol(conf);
  476. break;
  477. case LUAT_CODEC_SET_MIC_VOL:
  478. es8311_set_mic_vol(conf,(uint8_t)data);
  479. break;
  480. case LUAT_CODEC_GET_MIC_VOL:
  481. return es8311_get_mic_vol(conf);
  482. break;
  483. case LUAT_CODEC_SET_FORMAT:
  484. es8311_set_format(conf,(uint8_t)data);
  485. break;
  486. case LUAT_CODEC_SET_RATE:
  487. es8311_codec_samplerate(conf,(uint16_t)data);
  488. break;
  489. case LUAT_CODEC_SET_BITS:
  490. es8311_codec_samplebits(conf,(uint8_t)data);
  491. break;
  492. case LUAT_CODEC_SET_CHANNEL:
  493. es8311_codec_channels(conf,(uint8_t)data);
  494. break;
  495. default:
  496. break;
  497. }
  498. return 0;
  499. }
  500. static int es8311_codec_start(luat_audio_codec_conf_t* conf){
  501. es8311_mode_resume(conf,LUAT_CODEC_MODE_ALL);
  502. // luat_audio_pa(conf->multimedia_id,1, 0);
  503. return 0;
  504. }
  505. static int es8311_codec_stop(luat_audio_codec_conf_t* conf){
  506. // luat_audio_pa(conf->multimedia_id,0, 0);
  507. es8311_mode_standby(conf,LUAT_CODEC_MODE_ALL);
  508. return 0;
  509. }
  510. const luat_audio_codec_opts_t codec_opts_es8311 = {
  511. .name = "es8311",
  512. .init = es8311_codec_init,
  513. .deinit = es8311_codec_deinit,
  514. .control = es8311_codec_control,
  515. .start = es8311_codec_start,
  516. .stop = es8311_codec_stop,
  517. };