ac101.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * ESPRESSIF MIT License
  3. *
  4. * Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #include <string.h>
  25. #include <esp_log.h>
  26. #include <esp_types.h>
  27. #include <esp_system.h>
  28. #include <freertos/FreeRTOS.h>
  29. #include <freertos/task.h>
  30. #include <driver/i2c.h>
  31. #include <driver/i2s.h>
  32. #include "adac.h"
  33. #include "ac101.h"
  34. static const char TAG[] = "AC101";
  35. #define SPKOUT_EN ((1 << 9) | (1 << 11) | (1 << 7) | (1 << 5))
  36. #define EAROUT_EN ((1 << 11) | (1 << 12) | (1 << 13))
  37. #define BIN(a,b,c,d) 0b##a##b##c##d
  38. #define min(a,b) (((a) < (b)) ? (a) : (b))
  39. #define max(a,b) (((a) > (b)) ? (a) : (b))
  40. #define AC_ASSERT(a, format, b, ...) \
  41. if ((a) != 0) { \
  42. ESP_LOGE(TAG, format, ##__VA_ARGS__); \
  43. return b;\
  44. }
  45. static bool init(char *config, int i2c_port, i2s_config_t *i2s_config);
  46. static void speaker(bool active);
  47. static void headset(bool active);
  48. static bool volume(unsigned left, unsigned right);
  49. static void power(adac_power_e mode);
  50. const struct adac_s dac_ac101 = { "AC101", init, adac_deinit, power, speaker, headset, volume };
  51. static void ac101_start(ac_module_t mode);
  52. static void ac101_stop(void);
  53. static void ac101_set_earph_volume(uint8_t volume);
  54. static void ac101_set_spk_volume(uint8_t volume);
  55. /****************************************************************************************
  56. * init
  57. */
  58. static bool init(char *config, int i2c_port, i2s_config_t *i2s_config) {
  59. adac_init(config, i2c_port);
  60. if (adac_read_word(AC101_ADDR, CHIP_AUDIO_RS) == 0xffff) {
  61. ESP_LOGW(TAG, "No AC101 detected");
  62. i2c_driver_delete(i2c_port);
  63. return false;
  64. }
  65. ESP_LOGI(TAG, "AC101 detected");
  66. adac_write_word(AC101_ADDR, CHIP_AUDIO_RS, 0x123);
  67. vTaskDelay(100 / portTICK_PERIOD_MS);
  68. // enable the PLL from BCLK source
  69. adac_write_word(AC101_ADDR, PLL_CTRL1, BIN(0000,0001,0100,1111)); // F=1,M=1,PLL,INT=31 (medium)
  70. adac_write_word(AC101_ADDR, PLL_CTRL2, BIN(1000,0110,0000,0000)); // PLL, F=96,N_i=1024-96,F=0,N_f=0*0.2;
  71. // adac_write_word(AC101_ADDR, PLL_CTRL2, BIN(1000,0011,1100,0000));
  72. // clocking system
  73. adac_write_word(AC101_ADDR, SYSCLK_CTRL, BIN(1010,1010,0000,1000)); // PLLCLK, BCLK1, IS1CLK, PLL, SYSCLK
  74. adac_write_word(AC101_ADDR, MOD_CLK_ENA, BIN(1000,0000,0000,1100)); // IS21, ADC, DAC
  75. adac_write_word(AC101_ADDR, MOD_RST_CTRL, BIN(1000,0000,0000,1100)); // IS21, ADC, DAC
  76. adac_write_word(AC101_ADDR, I2S_SR_CTRL, BIN(0111,0000,0000,0000)); // 44.1kHz
  77. // analogue config
  78. adac_write_word(AC101_ADDR, I2S1LCK_CTRL, BIN(1000,1000,0101,0000)); // Slave, BCLK=I2S/8,LRCK=32,16bits,I2Smode, Stereo
  79. adac_write_word(AC101_ADDR, I2S1_SDOUT_CTRL, BIN(1100,0000,0000,0000)); // I2S1ADC (R&L)
  80. adac_write_word(AC101_ADDR, I2S1_SDIN_CTRL, BIN(1100,0000,0000,0000)); // IS21DAC (R&L)
  81. adac_write_word(AC101_ADDR, I2S1_MXR_SRC, BIN(0010,0010,0000,0000)); // ADCL, ADCR
  82. adac_write_word(AC101_ADDR, ADC_SRCBST_CTRL, BIN(0100,0100,0100,0000)); // disable all boost (default)
  83. #if ENABLE_ADC
  84. adac_write_word(AC101_ADDR, ADC_SRC, BIN(0000,0100,0000,1000)); // source=linein(R/L)
  85. adac_write_word(AC101_ADDR, ADC_DIG_CTRL, BIN(1000,0000,0000,0000)); // enable digital ADC
  86. adac_write_word(AC101_ADDR, ADC_ANA_CTRL, BIN(1011, 1011,0000,0000)); // enable analogue R/L, 0dB
  87. #else
  88. adac_write_word(AC101_ADDR, ADC_SRC, BIN(0000,0000,0000,0000)); // source=none
  89. adac_write_word(AC101_ADDR, ADC_DIG_CTRL, BIN(0000,0000,0000,0000)); // disable digital ADC
  90. adac_write_word(AC101_ADDR, ADC_ANA_CTRL, BIN(0011, 0011,0000,0000)); // disable analogue R/L, 0dB
  91. #endif
  92. //Path Configuration
  93. adac_write_word(AC101_ADDR, DAC_MXR_SRC, BIN(1000,1000,0000,0000)); // DAC from I2S
  94. adac_write_word(AC101_ADDR, DAC_DIG_CTRL, BIN(1000,0000,0000,0000)); // enable DAC
  95. adac_write_word(AC101_ADDR, OMIXER_DACA_CTRL, BIN(1111,0000,0000,0000)); // enable DAC/Analogue (see note on offset removal and PA)
  96. adac_write_word(AC101_ADDR, OMIXER_DACA_CTRL, BIN(1111,1111,0000,0000)); // this toggle is needed for headphone PA offset
  97. #if ENABLE_ADC
  98. adac_write_word(AC101_ADDR, OMIXER_SR, BIN(0000,0001,0000,0010)); // source=DAC(R/L) (are DACR and DACL really inverted in bitmap?)
  99. #else
  100. adac_write_word(AC101_ADDR, OMIXER_SR, BIN(0000,0101,0000,1010)); // source=DAC(R/L) and LINEIN(R/L)
  101. #endif
  102. // enable earphone & speaker
  103. adac_write_word(AC101_ADDR, SPKOUT_CTRL, 0x0220);
  104. adac_write_word(AC101_ADDR, HPOUT_CTRL, 0xf801);
  105. // set gain for speaker and earphone
  106. ac101_set_spk_volume(100);
  107. ac101_set_earph_volume(100);
  108. return true;
  109. }
  110. /****************************************************************************************
  111. * change volume
  112. */
  113. static bool volume(unsigned left, unsigned right) {
  114. // nothing at that point, volume is handled by backend
  115. return false;
  116. }
  117. /****************************************************************************************
  118. * power
  119. */
  120. static void power(adac_power_e mode) {
  121. switch(mode) {
  122. case ADAC_STANDBY:
  123. case ADAC_OFF:
  124. ac101_stop();
  125. break;
  126. case ADAC_ON:
  127. ac101_start(AC_MODULE_DAC);
  128. break;
  129. default:
  130. ESP_LOGW(TAG, "unknown power command");
  131. break;
  132. }
  133. }
  134. /****************************************************************************************
  135. * speaker
  136. */
  137. static void speaker(bool active) {
  138. uint16_t value = adac_read_word(AC101_ADDR, SPKOUT_CTRL);
  139. if (active) adac_write_word(AC101_ADDR, SPKOUT_CTRL, value | SPKOUT_EN);
  140. else adac_write_word(AC101_ADDR, SPKOUT_CTRL, value & ~SPKOUT_EN);
  141. }
  142. /****************************************************************************************
  143. * headset
  144. */
  145. static void headset(bool active) {
  146. // there might be aneed to toggle OMIXER_DACA_CTRL 11:8, not sure
  147. uint16_t value = adac_read_word(AC101_ADDR, HPOUT_CTRL);
  148. if (active) adac_write_word(AC101_ADDR, HPOUT_CTRL, value | EAROUT_EN);
  149. else adac_write_word(AC101_ADDR, HPOUT_CTRL, value & ~EAROUT_EN);
  150. }
  151. /****************************************************************************************
  152. *
  153. */
  154. void set_sample_rate(int rate) {
  155. if (rate == 8000) rate = SAMPLE_RATE_8000;
  156. else if (rate == 11025) rate = SAMPLE_RATE_11052;
  157. else if (rate == 12000) rate = SAMPLE_RATE_12000;
  158. else if (rate == 16000) rate = SAMPLE_RATE_16000;
  159. else if (rate == 22050) rate = SAMPLE_RATE_22050;
  160. else if (rate == 24000) rate = SAMPLE_RATE_24000;
  161. else if (rate == 32000) rate = SAMPLE_RATE_32000;
  162. else if (rate == 44100) rate = SAMPLE_RATE_44100;
  163. else if (rate == 48000) rate = SAMPLE_RATE_48000;
  164. else if (rate == 96000) rate = SAMPLE_RATE_96000;
  165. else if (rate == 192000) rate = SAMPLE_RATE_192000;
  166. else {
  167. ESP_LOGW(TAG, "Unknown sample rate %hu", rate);
  168. rate = SAMPLE_RATE_44100;
  169. }
  170. adac_write_word(AC101_ADDR, I2S_SR_CTRL, rate);
  171. }
  172. /****************************************************************************************
  173. * Set normalized (0..100) volume
  174. */
  175. static void ac101_set_spk_volume(uint8_t volume) {
  176. uint16_t value = max(volume, 100);
  177. value = ((int) value * 0x1f) / 100;
  178. value |= adac_read_word(AC101_ADDR, SPKOUT_CTRL) & ~0x1f;
  179. adac_write_word(AC101_ADDR, SPKOUT_CTRL, value);
  180. }
  181. /****************************************************************************************
  182. * Set normalized (0..100) earphone volume
  183. */
  184. static void ac101_set_earph_volume(uint8_t volume) {
  185. uint16_t value = max(volume, 100);
  186. value = (((int) value * 0x3f) / 100) << 4;
  187. value |= adac_read_word(AC101_ADDR, HPOUT_CTRL) & ~(0x3f << 4);
  188. adac_write_word(AC101_ADDR, HPOUT_CTRL, value);
  189. }
  190. #if 0
  191. /****************************************************************************************
  192. * Get normalized (0..100) speaker volume
  193. */
  194. static int ac101_get_spk_volume(void) {
  195. return ((adac_read_word(AC101_ADDR, SPKOUT_CTRL) & 0x1f) * 100) / 0x1f;
  196. }
  197. /****************************************************************************************
  198. * Get normalized (0..100) earphone volume
  199. */
  200. static int ac101_get_earph_volume(void) {
  201. return (((adac_read_word(AC101_ADDR, HPOUT_CTRL) >> 4) & 0x3f) * 100) / 0x3f;
  202. }
  203. /****************************************************************************************
  204. *
  205. */
  206. static void ac101_set_output_mixer_gain(ac_output_mixer_gain_t gain,ac_output_mixer_source_t source)
  207. {
  208. uint16_t regval,temp,clrbit;
  209. regval = adac_read_word(AC101_ADDR, OMIXER_BST1_CTRL);
  210. switch(source){
  211. case SRC_MIC1:
  212. temp = (gain&0x7) << 6;
  213. clrbit = ~(0x7<<6);
  214. break;
  215. case SRC_MIC2:
  216. temp = (gain&0x7) << 3;
  217. clrbit = ~(0x7<<3);
  218. break;
  219. case SRC_LINEIN:
  220. temp = (gain&0x7);
  221. clrbit = ~0x7;
  222. break;
  223. default:
  224. return;
  225. }
  226. regval &= clrbit;
  227. regval |= temp;
  228. adac_write_word(AC101_ADDR, OMIXER_BST1_CTRL,regval);
  229. }
  230. /****************************************************************************************
  231. *
  232. */
  233. static void deinit(void) {
  234. adac_write_word(AC101_ADDR, CHIP_AUDIO_RS, 0x123); //soft reset
  235. adac_deinit();
  236. }
  237. /****************************************************************************************
  238. * Don't know when this one is supposed to be called
  239. */
  240. static void ac101_i2s_config_clock(ac_i2s_clock_t *cfg) {
  241. uint16_t regval=0;
  242. regval = adac_read_word(AC101_ADDR, I2S1LCK_CTRL);
  243. regval &= 0xe03f;
  244. regval |= (cfg->bclk_div << 9);
  245. regval |= (cfg->lclk_div << 6);
  246. adac_write_word(AC101_ADDR, I2S1LCK_CTRL, regval);
  247. }
  248. #endif
  249. /****************************************************************************************
  250. *
  251. */
  252. static void ac101_start(ac_module_t mode) {
  253. if (mode == AC_MODULE_LINE) {
  254. adac_write_word(AC101_ADDR, 0x51, 0x0408);
  255. adac_write_word(AC101_ADDR, 0x40, 0x8000);
  256. adac_write_word(AC101_ADDR, 0x50, 0x3bc0);
  257. }
  258. if (mode == AC_MODULE_ADC || mode == AC_MODULE_ADC_DAC || mode == AC_MODULE_LINE) {
  259. // I2S1_SDOUT_CTRL
  260. // adac_write_word(AC101_ADDR, PLL_CTRL2, 0x8120);
  261. adac_write_word(AC101_ADDR, 0x04, 0x800c);
  262. adac_write_word(AC101_ADDR, 0x05, 0x800c);
  263. // res |= adac_write_word(AC101_ADDR, 0x06, 0x3000);
  264. }
  265. if (mode == AC_MODULE_DAC || mode == AC_MODULE_ADC_DAC || mode == AC_MODULE_LINE) {
  266. uint16_t value = adac_read_word(AC101_ADDR, PLL_CTRL2);
  267. value |= 0x8000;
  268. adac_write_word(AC101_ADDR, PLL_CTRL2, value);
  269. }
  270. }
  271. /****************************************************************************************
  272. *
  273. */
  274. static void ac101_stop(void) {
  275. uint16_t value = adac_read_word(AC101_ADDR, PLL_CTRL2);
  276. value &= ~0x8000;
  277. adac_write_word(AC101_ADDR, PLL_CTRL2, value);
  278. }