ac101.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #if BYTES_PER_FRAME == 8
  79. adac_write_word(AC101_ADDR, I2S1LCK_CTRL, BIN(1000,1000,0111,0000)); // Slave, BCLK=I2S/8,LRCK=32,24bits,I2Smode, Stereo
  80. i2s_config->bits_per_sample = 24;
  81. #else
  82. adac_write_word(AC101_ADDR, I2S1LCK_CTRL, BIN(1000,1000,0101,0000)); // Slave, BCLK=I2S/8,LRCK=32,16bits,I2Smode, Stereo
  83. #endif
  84. adac_write_word(AC101_ADDR, I2S1_SDOUT_CTRL, BIN(1100,0000,0000,0000)); // I2S1ADC (R&L)
  85. adac_write_word(AC101_ADDR, I2S1_SDIN_CTRL, BIN(1100,0000,0000,0000)); // IS21DAC (R&L)
  86. adac_write_word(AC101_ADDR, I2S1_MXR_SRC, BIN(0010,0010,0000,0000)); // ADCL, ADCR
  87. adac_write_word(AC101_ADDR, ADC_SRCBST_CTRL, BIN(0100,0100,0100,0000)); // disable all boost (default)
  88. #if ENABLE_ADC
  89. adac_write_word(AC101_ADDR, ADC_SRC, BIN(0000,0100,0000,1000)); // source=linein(R/L)
  90. adac_write_word(AC101_ADDR, ADC_DIG_CTRL, BIN(1000,0000,0000,0000)); // enable digital ADC
  91. adac_write_word(AC101_ADDR, ADC_ANA_CTRL, BIN(1011, 1011,0000,0000)); // enable analogue R/L, 0dB
  92. #else
  93. adac_write_word(AC101_ADDR, ADC_SRC, BIN(0000,0000,0000,0000)); // source=none
  94. adac_write_word(AC101_ADDR, ADC_DIG_CTRL, BIN(0000,0000,0000,0000)); // disable digital ADC
  95. adac_write_word(AC101_ADDR, ADC_ANA_CTRL, BIN(0011, 0011,0000,0000)); // disable analogue R/L, 0dB
  96. #endif
  97. //Path Configuration
  98. adac_write_word(AC101_ADDR, DAC_MXR_SRC, BIN(1000,1000,0000,0000)); // DAC from I2S
  99. adac_write_word(AC101_ADDR, DAC_DIG_CTRL, BIN(1000,0000,0000,0000)); // enable DAC
  100. adac_write_word(AC101_ADDR, OMIXER_DACA_CTRL, BIN(1111,0000,0000,0000)); // enable DAC/Analogue (see note on offset removal and PA)
  101. adac_write_word(AC101_ADDR, OMIXER_DACA_CTRL, BIN(1111,1111,0000,0000)); // this toggle is needed for headphone PA offset
  102. #if ENABLE_ADC
  103. adac_write_word(AC101_ADDR, OMIXER_SR, BIN(0000,0001,0000,0010)); // source=DAC(R/L) (are DACR and DACL really inverted in bitmap?)
  104. #else
  105. adac_write_word(AC101_ADDR, OMIXER_SR, BIN(0000,0101,0000,1010)); // source=DAC(R/L) and LINEIN(R/L)
  106. #endif
  107. // enable earphone & speaker
  108. adac_write_word(AC101_ADDR, SPKOUT_CTRL, 0x0220);
  109. adac_write_word(AC101_ADDR, HPOUT_CTRL, 0xf801);
  110. // set gain for speaker and earphone
  111. ac101_set_spk_volume(100);
  112. ac101_set_earph_volume(100);
  113. return true;
  114. }
  115. /****************************************************************************************
  116. * change volume
  117. */
  118. static bool volume(unsigned left, unsigned right) {
  119. // nothing at that point, volume is handled by backend
  120. return false;
  121. }
  122. /****************************************************************************************
  123. * power
  124. */
  125. static void power(adac_power_e mode) {
  126. switch(mode) {
  127. case ADAC_STANDBY:
  128. case ADAC_OFF:
  129. ac101_stop();
  130. break;
  131. case ADAC_ON:
  132. ac101_start(AC_MODULE_DAC);
  133. break;
  134. default:
  135. ESP_LOGW(TAG, "unknown power command");
  136. break;
  137. }
  138. }
  139. /****************************************************************************************
  140. * speaker
  141. */
  142. static void speaker(bool active) {
  143. uint16_t value = adac_read_word(AC101_ADDR, SPKOUT_CTRL);
  144. if (active) adac_write_word(AC101_ADDR, SPKOUT_CTRL, value | SPKOUT_EN);
  145. else adac_write_word(AC101_ADDR, SPKOUT_CTRL, value & ~SPKOUT_EN);
  146. }
  147. /****************************************************************************************
  148. * headset
  149. */
  150. static void headset(bool active) {
  151. // there might be aneed to toggle OMIXER_DACA_CTRL 11:8, not sure
  152. uint16_t value = adac_read_word(AC101_ADDR, HPOUT_CTRL);
  153. if (active) adac_write_word(AC101_ADDR, HPOUT_CTRL, value | EAROUT_EN);
  154. else adac_write_word(AC101_ADDR, HPOUT_CTRL, value & ~EAROUT_EN);
  155. }
  156. /****************************************************************************************
  157. *
  158. */
  159. void set_sample_rate(int rate) {
  160. if (rate == 8000) rate = SAMPLE_RATE_8000;
  161. else if (rate == 11025) rate = SAMPLE_RATE_11052;
  162. else if (rate == 12000) rate = SAMPLE_RATE_12000;
  163. else if (rate == 16000) rate = SAMPLE_RATE_16000;
  164. else if (rate == 22050) rate = SAMPLE_RATE_22050;
  165. else if (rate == 24000) rate = SAMPLE_RATE_24000;
  166. else if (rate == 32000) rate = SAMPLE_RATE_32000;
  167. else if (rate == 44100) rate = SAMPLE_RATE_44100;
  168. else if (rate == 48000) rate = SAMPLE_RATE_48000;
  169. else if (rate == 96000) rate = SAMPLE_RATE_96000;
  170. else if (rate == 192000) rate = SAMPLE_RATE_192000;
  171. else {
  172. ESP_LOGW(TAG, "Unknown sample rate %hu", rate);
  173. rate = SAMPLE_RATE_44100;
  174. }
  175. adac_write_word(AC101_ADDR, I2S_SR_CTRL, rate);
  176. }
  177. /****************************************************************************************
  178. * Set normalized (0..100) volume
  179. */
  180. static void ac101_set_spk_volume(uint8_t volume) {
  181. uint16_t value = max(volume, 100);
  182. value = ((int) value * 0x1f) / 100;
  183. value |= adac_read_word(AC101_ADDR, SPKOUT_CTRL) & ~0x1f;
  184. adac_write_word(AC101_ADDR, SPKOUT_CTRL, value);
  185. }
  186. /****************************************************************************************
  187. * Set normalized (0..100) earphone volume
  188. */
  189. static void ac101_set_earph_volume(uint8_t volume) {
  190. uint16_t value = max(volume, 100);
  191. value = (((int) value * 0x3f) / 100) << 4;
  192. value |= adac_read_word(AC101_ADDR, HPOUT_CTRL) & ~(0x3f << 4);
  193. adac_write_word(AC101_ADDR, HPOUT_CTRL, value);
  194. }
  195. #if 0
  196. /****************************************************************************************
  197. * Get normalized (0..100) speaker volume
  198. */
  199. static int ac101_get_spk_volume(void) {
  200. return ((adac_read_word(AC101_ADDR, SPKOUT_CTRL) & 0x1f) * 100) / 0x1f;
  201. }
  202. /****************************************************************************************
  203. * Get normalized (0..100) earphone volume
  204. */
  205. static int ac101_get_earph_volume(void) {
  206. return (((adac_read_word(AC101_ADDR, HPOUT_CTRL) >> 4) & 0x3f) * 100) / 0x3f;
  207. }
  208. /****************************************************************************************
  209. *
  210. */
  211. static void ac101_set_output_mixer_gain(ac_output_mixer_gain_t gain,ac_output_mixer_source_t source)
  212. {
  213. uint16_t regval,temp,clrbit;
  214. regval = adac_read_word(AC101_ADDR, OMIXER_BST1_CTRL);
  215. switch(source){
  216. case SRC_MIC1:
  217. temp = (gain&0x7) << 6;
  218. clrbit = ~(0x7<<6);
  219. break;
  220. case SRC_MIC2:
  221. temp = (gain&0x7) << 3;
  222. clrbit = ~(0x7<<3);
  223. break;
  224. case SRC_LINEIN:
  225. temp = (gain&0x7);
  226. clrbit = ~0x7;
  227. break;
  228. default:
  229. return;
  230. }
  231. regval &= clrbit;
  232. regval |= temp;
  233. adac_write_word(AC101_ADDR, OMIXER_BST1_CTRL,regval);
  234. }
  235. /****************************************************************************************
  236. *
  237. */
  238. static void deinit(void) {
  239. adac_write_word(AC101_ADDR, CHIP_AUDIO_RS, 0x123); //soft reset
  240. adac_deinit();
  241. }
  242. /****************************************************************************************
  243. * Don't know when this one is supposed to be called
  244. */
  245. static void ac101_i2s_config_clock(ac_i2s_clock_t *cfg) {
  246. uint16_t regval=0;
  247. regval = adac_read_word(AC101_ADDR, I2S1LCK_CTRL);
  248. regval &= 0xe03f;
  249. regval |= (cfg->bclk_div << 9);
  250. regval |= (cfg->lclk_div << 6);
  251. adac_write_word(AC101_ADDR, I2S1LCK_CTRL, regval);
  252. }
  253. #endif
  254. /****************************************************************************************
  255. *
  256. */
  257. static void ac101_start(ac_module_t mode) {
  258. if (mode == AC_MODULE_LINE) {
  259. adac_write_word(AC101_ADDR, 0x51, 0x0408);
  260. adac_write_word(AC101_ADDR, 0x40, 0x8000);
  261. adac_write_word(AC101_ADDR, 0x50, 0x3bc0);
  262. }
  263. if (mode == AC_MODULE_ADC || mode == AC_MODULE_ADC_DAC || mode == AC_MODULE_LINE) {
  264. // I2S1_SDOUT_CTRL
  265. // adac_write_word(AC101_ADDR, PLL_CTRL2, 0x8120);
  266. adac_write_word(AC101_ADDR, 0x04, 0x800c);
  267. adac_write_word(AC101_ADDR, 0x05, 0x800c);
  268. // res |= adac_write_word(AC101_ADDR, 0x06, 0x3000);
  269. }
  270. if (mode == AC_MODULE_DAC || mode == AC_MODULE_ADC_DAC || mode == AC_MODULE_LINE) {
  271. uint16_t value = adac_read_word(AC101_ADDR, PLL_CTRL2);
  272. value |= 0x8000;
  273. adac_write_word(AC101_ADDR, PLL_CTRL2, value);
  274. }
  275. }
  276. /****************************************************************************************
  277. *
  278. */
  279. static void ac101_stop(void) {
  280. uint16_t value = adac_read_word(AC101_ADDR, PLL_CTRL2);
  281. value &= ~0x8000;
  282. adac_write_word(AC101_ADDR, PLL_CTRL2, value);
  283. }