ac101.c 11 KB

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