luat_audio_es8311.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. es8311_dacvol_bak = es8311_read_reg(conf,ES8311_DAC_REG32);
  236. es8311_adcvol_bak = es8311_read_reg(conf,ES8311_ADC_REG17);
  237. es8311_write_reg(conf,ES8311_DAC_REG32,0x00);
  238. es8311_write_reg(conf,ES8311_ADC_REG17,0x00);
  239. es8311_write_reg(conf,ES8311_SYSTEM_REG0E,0xff);
  240. es8311_write_reg(conf,ES8311_SYSTEM_REG12,0x02);
  241. es8311_write_reg(conf,ES8311_SYSTEM_REG14,0x00);
  242. es8311_write_reg(conf,ES8311_SYSTEM_REG0D,0xf9);
  243. es8311_write_reg(conf,ES8311_ADC_REG15,0x00);
  244. es8311_write_reg(conf,ES8311_DAC_REG37,0x08);
  245. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG02,0x10);
  246. es8311_write_reg(conf,ES8311_RESET_REG00,0x00);
  247. es8311_write_reg(conf,ES8311_RESET_REG00,0x1f);
  248. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x30);
  249. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x00);
  250. es8311_write_reg(conf,ES8311_GP_REG45,0x00);
  251. es8311_write_reg(conf,ES8311_SYSTEM_REG0D,0xfc);
  252. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG02,0x00);
  253. return 0;
  254. }
  255. // mute
  256. static uint8_t es8311_set_mute(luat_audio_codec_conf_t* conf,uint8_t enable){
  257. es8311_write_reg(conf,ES8311_DAC_REG31, enable?0x20:0x00);
  258. return 0;
  259. }
  260. static uint8_t es8311_get_mute(luat_audio_codec_conf_t* conf){
  261. uint8_t reg = es8311_read_reg(conf,ES8311_DAC_REG31);
  262. return (reg & 0x20) >> 5;
  263. }
  264. // voice_vol
  265. static uint8_t es8311_set_voice_vol(luat_audio_codec_conf_t* conf,uint8_t vol){
  266. if(vol < 0 || vol > 100) return -1;
  267. es8311_write_reg(conf,ES8311_DAC_REG32,(uint8_t)(vol * 2550 / 1000));
  268. return 0;
  269. }
  270. static uint8_t es8311_get_voice_vol(luat_audio_codec_conf_t* conf){
  271. uint8_t reg = es8311_read_reg(conf,ES8311_DAC_REG32);
  272. return reg * 1000 / 2550;
  273. }
  274. // mic_vol
  275. static uint8_t es8311_set_mic_vol(luat_audio_codec_conf_t* conf,uint8_t vol){
  276. if(vol < 0 || vol > 100) return -1;
  277. es8311_write_reg(conf,ES8311_ADC_REG17,(uint8_t)(vol * 2550 / 1000));
  278. return 0;
  279. }
  280. static uint8_t es8311_get_mic_vol(luat_audio_codec_conf_t* conf){
  281. uint8_t reg = es8311_read_reg(conf,ES8311_ADC_REG17);
  282. return reg * 1000 / 2550;
  283. }
  284. static int es8311_codec_samplerate(luat_audio_codec_conf_t* conf,uint16_t samplerate){
  285. if(samplerate != 8000 && samplerate != 16000 && samplerate != 32000 &&
  286. samplerate != 11025 && samplerate != 22050 && samplerate != 44100 &&
  287. samplerate != 12000 && samplerate != 24000 && samplerate != 48000)
  288. {
  289. LLOGE("samplerate error! samplerate:%d\n",samplerate);
  290. return -1;
  291. }
  292. int coeff = es8311_get_coeff(samplerate * MCLK_DIV_FRE, samplerate);
  293. if (coeff < 0){
  294. LLOGE("Unable to configure sample rate %dHz with %dHz MCLK", samplerate, samplerate * MCLK_DIV_FRE);
  295. return -1;
  296. }
  297. uint8_t reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG02) & 0x07;
  298. reg |= (es8311_codec_div[coeff].preDiv - 1) << 5;
  299. uint8_t datmp = 0;
  300. switch (es8311_codec_div[coeff].preMulti){
  301. case 1:
  302. datmp = 0;
  303. break;
  304. case 2:
  305. datmp = 1;
  306. break;
  307. case 4:
  308. datmp = 2;
  309. break;
  310. case 8:
  311. datmp = 3;
  312. break;
  313. default:
  314. break;
  315. }
  316. reg |= (datmp) << 3;
  317. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG02, reg);
  318. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG05) & 0x00;
  319. reg |= (es8311_codec_div[coeff].adcDiv - 1) << 4;
  320. reg |= (es8311_codec_div[coeff].dacDiv - 1) << 0;
  321. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG05, reg);
  322. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG03) & 0x80;
  323. reg |= es8311_codec_div[coeff].fsMode << 6;
  324. reg |= es8311_codec_div[coeff].adcOsr << 0;
  325. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG03, reg);
  326. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG04) & 0x80;
  327. reg |= es8311_codec_div[coeff].dacOsr << 0;
  328. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG04, reg);
  329. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG07) & 0xC0;
  330. reg |= es8311_codec_div[coeff].lrckH << 0;
  331. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG07, reg);
  332. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG08) & 0x00;
  333. reg |= es8311_codec_div[coeff].lrckL << 0;
  334. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG08, reg);
  335. reg = es8311_read_reg(conf,ES8311_CLK_MANAGER_REG06) & 0xE0;
  336. if (es8311_codec_div[coeff].bclkDiv < 19)
  337. reg |= (es8311_codec_div[coeff].bclkDiv - 1) << 0;
  338. else
  339. reg |= (es8311_codec_div[coeff].bclkDiv) << 0;
  340. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG06, reg);
  341. return 0;
  342. }
  343. static int es8311_set_format(luat_audio_codec_conf_t* conf,uint8_t format){
  344. // only support I2S format now
  345. uint8_t dacIface = es8311_read_reg(conf,ES8311_SDPIN_REG09);
  346. uint8_t adcIface = es8311_read_reg(conf,ES8311_SDPOUT_REG0A);
  347. dacIface &= 0xFC;
  348. adcIface &= 0xFC;
  349. es8311_write_reg(conf,ES8311_SDPIN_REG09, dacIface);
  350. es8311_write_reg(conf,ES8311_SDPOUT_REG0A, adcIface);
  351. return 0;
  352. }
  353. static int es8311_codec_samplebits(luat_audio_codec_conf_t* conf,uint8_t samplebits){
  354. if(samplebits != 8 && samplebits != 16 && samplebits != 24 && samplebits != 32){
  355. LLOGE("bit_width error!\n");
  356. return -1;
  357. }
  358. int wl;
  359. switch (samplebits){
  360. case 16:
  361. wl = 3;
  362. break;
  363. case 18:
  364. wl = 2;
  365. break;
  366. case 20:
  367. wl = 1;
  368. break;
  369. case 24:
  370. wl = 0;
  371. break;
  372. case 32:
  373. wl = 4;
  374. break;
  375. default:
  376. return -1;
  377. }
  378. uint8_t dac_iface = es8311_read_reg(conf,ES8311_SDPIN_REG09);
  379. uint8_t adc_iface = es8311_read_reg(conf,ES8311_SDPOUT_REG0A);
  380. dac_iface |= wl << ES8311_SDPIN_REG09_DACWL_SHIFT;
  381. adc_iface |= wl << ES8311_SDPOUT_REG0A_ADCWL_SHIFT;
  382. es8311_write_reg(conf,ES8311_SDPIN_REG09, dac_iface);
  383. es8311_write_reg(conf,ES8311_SDPOUT_REG0A, adc_iface);
  384. return 0;
  385. }
  386. static int es8311_codec_channels(luat_audio_codec_conf_t* conf,uint8_t channels){
  387. return 0;
  388. }
  389. static inline void es8311_reset(luat_audio_codec_conf_t* conf){
  390. es8311_write_reg(conf,ES8311_RESET_REG00, 0x1F);
  391. es8311_write_reg(conf,ES8311_RESET_REG00, 0x80);
  392. luat_rtos_task_sleep(1);
  393. es8311_write_reg(conf,ES8311_SYSTEM_REG0D, 0x01);
  394. }
  395. static int es8311_codec_init(luat_audio_codec_conf_t* conf,uint8_t mode){
  396. luat_audio_power(conf->multimedia_id,1);
  397. luat_rtos_task_sleep(50);
  398. uint8_t temp1 = es8311_read_reg(conf,ES8311_CHD1_REGFD);
  399. uint8_t temp2 = es8311_read_reg(conf,ES8311_CHD2_REGFE);
  400. uint8_t temp3 = es8311_read_reg(conf,ES8311_CHVER_REGFF);
  401. if(temp1 != 0x83 || temp2 != 0x11){
  402. LLOGE("codec err, id = 0x%x 0x%x ver = 0x%x", temp1, temp2, temp3);
  403. return -1;
  404. }
  405. /* reset codec */
  406. es8311_reset(conf);
  407. // BCLK/LRCK pullup on
  408. es8311_write_reg(conf,ES8311_GP_REG45, 0x00);
  409. /* power up digital */
  410. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01, 0x30);
  411. // SET SDP in and SDP out mute
  412. es8311_write_reg(conf,ES8311_SDPIN_REG09, (es8311_read_reg(conf,ES8311_SDPIN_REG09) & 0xBF));
  413. es8311_write_reg(conf,ES8311_SDPOUT_REG0A, (es8311_read_reg(conf,ES8311_SDPOUT_REG0A) & 0xBF));
  414. es8311_write_reg(conf,ES8311_SYSTEM_REG0B, 0x00);
  415. es8311_write_reg(conf,ES8311_SYSTEM_REG0C, 0x00);
  416. es8311_write_reg(conf,ES8311_SYSTEM_REG10, (0x1C*ES8311_DAC_HP_ON) + (0x60*ES8311_VDDA_VOLTAGE) + 0x03);
  417. es8311_write_reg(conf,ES8311_SYSTEM_REG11, 0x7F);
  418. es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x3F + (ES8311_MCLK_SOURCE<<7));
  419. es8311_write_reg(conf,ES8311_RESET_REG00, 0X80+(mode<<6));
  420. luat_rtos_task_sleep(1);
  421. es8311_write_reg(conf,ES8311_SYSTEM_REG0D, 0x01);
  422. es8311_write_reg(conf,ES8311_SYSTEM_REG14,(ES8311_DMIC_SEL<<6) + (ES8311_LINSEL_SEL<<4) + ES8311_ADC_PGA_GAIN);
  423. es8311_write_reg(conf,ES8311_SYSTEM_REG12, 0x28);
  424. es8311_write_reg(conf,ES8311_SYSTEM_REG13, 0x00 + (ES8311_DAC_HP_ON<<4));
  425. /* set normal power mode */
  426. es8311_write_reg(conf,ES8311_SYSTEM_REG0E, 0x02);
  427. es8311_write_reg(conf,ES8311_SYSTEM_REG0F, 0x44);
  428. // start up vmid normal speed charge
  429. es8311_write_reg(conf,ES8311_ADC_REG15, 0x00);
  430. /* set adc hpf */
  431. es8311_write_reg(conf,ES8311_ADC_REG1B, 0x0A);
  432. /* set adc hpf,ADC_EQ bypass */
  433. es8311_write_reg(conf,ES8311_ADC_REG1C, 0x6A);
  434. /* set dac softramp,disable DAC_EQ */
  435. es8311_write_reg(conf,ES8311_DAC_REG37, 0x08);
  436. // set internal reference signal (ADC + DAC)
  437. es8311_write_reg(conf,ES8311_GPIO_REG44, (ES8311_ADC2DAC_SEL<<7));
  438. // adc vol
  439. es8311_write_reg(conf,ES8311_ADC_REG17, es8311_adcvol_bak);
  440. // dac vol
  441. es8311_write_reg(conf,ES8311_DAC_REG32, es8311_dacvol_bak);
  442. return 0;
  443. }
  444. static int es8311_codec_deinit(luat_audio_codec_conf_t* conf){
  445. return 0;
  446. }
  447. static int es8311_codec_control(luat_audio_codec_conf_t* conf,luat_audio_codec_ctl_t cmd,uint32_t data){
  448. switch (cmd){
  449. case LUAT_CODEC_MODE_RESUME:
  450. es8311_mode_resume(conf,(uint8_t)data);
  451. break;
  452. case LUAT_CODEC_MODE_STANDBY:
  453. es8311_mode_standby(conf,(uint8_t)data);
  454. break;
  455. case LUAT_CODEC_MODE_PWRDOWN:
  456. es8311_mode_pwrdown(conf);
  457. break;
  458. case LUAT_CODEC_SET_MUTE:
  459. es8311_set_mute(conf,(uint8_t)data);
  460. break;
  461. case LUAT_CODEC_GET_MUTE:
  462. return es8311_get_mute(conf);
  463. break;
  464. case LUAT_CODEC_SET_VOICE_VOL:
  465. es8311_set_voice_vol(conf,(uint8_t)data);
  466. break;
  467. case LUAT_CODEC_GET_VOICE_VOL:
  468. return es8311_get_voice_vol(conf);
  469. break;
  470. case LUAT_CODEC_SET_MIC_VOL:
  471. es8311_set_mic_vol(conf,(uint8_t)data);
  472. break;
  473. case LUAT_CODEC_GET_MIC_VOL:
  474. return es8311_get_mic_vol(conf);
  475. break;
  476. case LUAT_CODEC_SET_FORMAT:
  477. es8311_set_format(conf,(uint8_t)data);
  478. break;
  479. case LUAT_CODEC_SET_RATE:
  480. es8311_codec_samplerate(conf,(uint16_t)data);
  481. break;
  482. case LUAT_CODEC_SET_BITS:
  483. es8311_codec_samplebits(conf,(uint8_t)data);
  484. break;
  485. case LUAT_CODEC_SET_CHANNEL:
  486. es8311_codec_channels(conf,(uint8_t)data);
  487. break;
  488. default:
  489. break;
  490. }
  491. return 0;
  492. }
  493. static int es8311_codec_start(luat_audio_codec_conf_t* conf){
  494. es8311_mode_resume(conf,LUAT_CODEC_MODE_ALL);
  495. luat_audio_pa(conf->multimedia_id,1, 0);
  496. return 0;
  497. }
  498. static int es8311_codec_stop(luat_audio_codec_conf_t* conf){
  499. luat_audio_pa(conf->multimedia_id,0, 0);
  500. es8311_mode_standby(conf,LUAT_CODEC_MODE_ALL);
  501. return 0;
  502. }
  503. const luat_audio_codec_opts_t codec_opts_es8311 = {
  504. .name = "es8311",
  505. .init = es8311_codec_init,
  506. .deinit = es8311_codec_deinit,
  507. .control = es8311_codec_control,
  508. .start = es8311_codec_start,
  509. .stop = es8311_codec_stop,
  510. };