ac101.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 "ac101.h"
  25. #include <driver/i2c.h>
  26. #include <driver/i2s.h>
  27. #include <esp_log.h>
  28. #include <esp_system.h>
  29. #include <esp_types.h>
  30. #include <freertos/FreeRTOS.h>
  31. #include <freertos/task.h>
  32. #include <string.h>
  33. #include "adac.h"
  34. const static 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(int i2c_port_num, int i2s_num, i2s_config_t* config);
  46. static void deinit(void);
  47. static void speaker(bool active);
  48. static void headset(bool active);
  49. static void volume(unsigned left, unsigned right);
  50. static void power(adac_power_e mode);
  51. struct adac_s dac_a1s = {init, deinit, power, speaker, headset, volume};
  52. static esp_err_t i2c_write_reg(uint8_t reg, uint16_t val);
  53. static uint16_t i2c_read_reg(uint8_t reg);
  54. static void ac101_start(ac_module_t mode);
  55. static void ac101_stop(void);
  56. static void ac101_set_earph_volume(uint8_t volume);
  57. static void ac101_set_spk_volume(uint8_t volume);
  58. static int ac101_get_spk_volume(void);
  59. static int i2c_port;
  60. /****************************************************************************************
  61. * init
  62. */
  63. static bool init(int i2c_port_num, int i2s_num, i2s_config_t* i2s_config) {
  64. esp_err_t res = ESP_OK;
  65. i2c_port = i2c_port_num;
  66. // configure i2c
  67. i2c_config_t i2c_config = {
  68. .mode = I2C_MODE_MASTER,
  69. .sda_io_num = 33,
  70. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  71. .scl_io_num = 32,
  72. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  73. .master.clk_speed = 250000,
  74. };
  75. i2c_param_config(i2c_port, &i2c_config);
  76. i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  77. res = i2c_read_reg(CHIP_AUDIO_RS);
  78. if (!res) {
  79. ESP_LOGW(TAG, "No AC101 detected");
  80. i2c_driver_delete(i2c_port);
  81. return 0;
  82. }
  83. ESP_LOGI(TAG, "AC101 DAC using I2C sda:%u, scl:%u", i2c_config.sda_io_num,
  84. i2c_config.scl_io_num);
  85. res = i2c_write_reg(CHIP_AUDIO_RS, 0x123);
  86. // huh?
  87. vTaskDelay(100 / portTICK_PERIOD_MS);
  88. // enable the PLL from BCLK source
  89. i2c_write_reg(PLL_CTRL1,
  90. BIN(0000, 0001, 0100, 1111)); // F=1,M=1,PLL,INT=31 (medium)
  91. i2c_write_reg(PLL_CTRL2, BIN(1000, 0110, 0000,
  92. 0000)); // PLL, F=96,N_i=1024-96,F=0,N_f=0*0.2;
  93. // i2c_write_reg(PLL_CTRL2, BIN(1000,0011,1100,0000));
  94. // clocking system
  95. i2c_write_reg(SYSCLK_CTRL, BIN(1010, 1010, 0000,
  96. 1000)); // PLLCLK, BCLK1, IS1CLK, PLL, SYSCLK
  97. i2c_write_reg(MOD_CLK_ENA, BIN(1000, 0000, 0000, 1100)); // IS21, ADC, DAC
  98. i2c_write_reg(MOD_RST_CTRL, BIN(1000, 0000, 0000, 1100)); // IS21, ADC, DAC
  99. i2c_write_reg(I2S_SR_CTRL, BIN(0111, 0000, 0000, 0000)); // 44.1kHz
  100. // analogue config
  101. i2c_write_reg(I2S1LCK_CTRL,
  102. BIN(1000, 1000, 0101,
  103. 0000)); // Slave, BCLK=I2S/8,LRCK=32,16bits,I2Smode, Stereo
  104. i2c_write_reg(I2S1_SDOUT_CTRL, BIN(1100, 0000, 0000, 0000)); // I2S1ADC (R&L)
  105. i2c_write_reg(I2S1_SDIN_CTRL, BIN(1100, 0000, 0000, 0000)); // IS21DAC (R&L)
  106. i2c_write_reg(I2S1_MXR_SRC, BIN(0010, 0010, 0000, 0000)); // ADCL, ADCR
  107. i2c_write_reg(ADC_SRCBST_CTRL,
  108. BIN(0100, 0100, 0100, 0000)); // disable all boost (default)
  109. #if ENABLE_ADC
  110. i2c_write_reg(ADC_SRC, BIN(0000, 0100, 0000, 1000)); // source=linein(R/L)
  111. i2c_write_reg(ADC_DIG_CTRL,
  112. BIN(1000, 0000, 0000, 0000)); // enable digital ADC
  113. i2c_write_reg(ADC_ANA_CTRL,
  114. BIN(1011, 1011, 0000, 0000)); // enable analogue R/L, 0dB
  115. #else
  116. i2c_write_reg(ADC_SRC, BIN(0000, 0000, 0000, 0000)); // source=none
  117. i2c_write_reg(ADC_DIG_CTRL,
  118. BIN(0000, 0000, 0000, 0000)); // disable digital ADC
  119. i2c_write_reg(ADC_ANA_CTRL,
  120. BIN(0011, 0011, 0000, 0000)); // disable analogue R/L, 0dB
  121. #endif
  122. //Path Configuration
  123. i2c_write_reg(DAC_MXR_SRC, BIN(1000, 1000, 0000, 0000)); // DAC from I2S
  124. i2c_write_reg(DAC_DIG_CTRL, BIN(1000, 0000, 0000, 0000)); // enable DAC
  125. i2c_write_reg(
  126. OMIXER_DACA_CTRL,
  127. BIN(1111, 0000, 0000,
  128. 0000)); // enable DAC/Analogue (see note on offset removal and PA)
  129. i2c_write_reg(OMIXER_DACA_CTRL,
  130. BIN(1111, 1111, 0000,
  131. 0000)); // this toggle is needed for headphone PA offset
  132. #if ENABLE_ADC
  133. i2c_write_reg(
  134. OMIXER_SR,
  135. BIN(0000, 0001, 0000,
  136. 0010)); // source=DAC(R/L) (are DACR and DACL really inverted in bitmap?)
  137. #else
  138. i2c_write_reg(OMIXER_SR, BIN(0000, 0101, 0000,
  139. 1010)); // source=DAC(R/L) and LINEIN(R/L)
  140. #endif
  141. // configure I2S pins & install driver
  142. i2s_pin_config_t i2s_pin_config = (i2s_pin_config_t){
  143. .bck_io_num = 27, .ws_io_num = 26, .data_out_num = 25, .data_in_num = -1};
  144. res |= i2s_driver_install(i2s_num, i2s_config, 0, NULL);
  145. res |= i2s_set_pin(i2s_num, &i2s_pin_config);
  146. // enable earphone & speaker
  147. i2c_write_reg(SPKOUT_CTRL, 0x0220);
  148. i2c_write_reg(HPOUT_CTRL, 0xf801);
  149. // set gain for speaker and earphone
  150. ac101_set_spk_volume(70);
  151. ac101_set_earph_volume(70);
  152. ESP_LOGI(TAG, "DAC using I2S bck:%d, ws:%d, do:%d", i2s_pin_config.bck_io_num,
  153. i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
  154. return (res == ESP_OK);
  155. }
  156. /****************************************************************************************
  157. * init
  158. */
  159. static void deinit(void) {
  160. i2c_driver_delete(i2c_port);
  161. }
  162. /****************************************************************************************
  163. * change volume
  164. */
  165. static void volume(unsigned left, unsigned right) {
  166. ac101_set_earph_volume(left);
  167. // nothing at that point, volume is handled by backend
  168. }
  169. /****************************************************************************************
  170. * power
  171. */
  172. static void power(adac_power_e mode) {
  173. switch (mode) {
  174. case ADAC_STANDBY:
  175. case ADAC_OFF:
  176. ac101_stop();
  177. break;
  178. case ADAC_ON:
  179. ac101_start(AC_MODULE_DAC);
  180. break;
  181. default:
  182. ESP_LOGW(TAG, "unknown power command");
  183. break;
  184. }
  185. }
  186. /****************************************************************************************
  187. * speaker
  188. */
  189. static void speaker(bool active) {
  190. uint16_t value = i2c_read_reg(SPKOUT_CTRL);
  191. if (active)
  192. i2c_write_reg(SPKOUT_CTRL, value | SPKOUT_EN);
  193. else
  194. i2c_write_reg(SPKOUT_CTRL, value & ~SPKOUT_EN);
  195. }
  196. /****************************************************************************************
  197. * headset
  198. */
  199. static void headset(bool active) {
  200. // there might be aneed to toggle OMIXER_DACA_CTRL 11:8, not sure
  201. uint16_t value = i2c_read_reg(HPOUT_CTRL);
  202. if (active)
  203. i2c_write_reg(HPOUT_CTRL, value | EAROUT_EN);
  204. else
  205. i2c_write_reg(HPOUT_CTRL, value & ~EAROUT_EN);
  206. }
  207. /****************************************************************************************
  208. *
  209. */
  210. static esp_err_t i2c_write_reg(uint8_t reg, uint16_t val) {
  211. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  212. esp_err_t ret = 0;
  213. uint8_t send_buff[4];
  214. send_buff[0] = (AC101_ADDR << 1);
  215. send_buff[1] = reg;
  216. send_buff[2] = (val >> 8) & 0xff;
  217. send_buff[3] = val & 0xff;
  218. ret |= i2c_master_start(cmd);
  219. ret |= i2c_master_write(cmd, send_buff, 4, ACK_CHECK_EN);
  220. ret |= i2c_master_stop(cmd);
  221. ret |= i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_PERIOD_MS);
  222. i2c_cmd_link_delete(cmd);
  223. return ret;
  224. }
  225. /****************************************************************************************
  226. *
  227. */
  228. static uint16_t i2c_read_reg(uint8_t reg) {
  229. uint8_t data[2] = {0};
  230. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  231. i2c_master_start(cmd);
  232. i2c_master_write_byte(cmd, (AC101_ADDR << 1) | WRITE_BIT, ACK_CHECK_EN);
  233. i2c_master_write_byte(cmd, reg, ACK_CHECK_EN);
  234. i2c_master_start(cmd);
  235. i2c_master_write_byte(cmd, (AC101_ADDR << 1) | READ_BIT,
  236. ACK_CHECK_EN); //check or not
  237. i2c_master_read(cmd, data, 2, ACK_VAL);
  238. i2c_master_stop(cmd);
  239. i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_PERIOD_MS);
  240. i2c_cmd_link_delete(cmd);
  241. return (data[0] << 8) + data[1];
  242. ;
  243. }
  244. /****************************************************************************************
  245. *
  246. */
  247. void set_sample_rate(int rate) {
  248. if (rate == 8000)
  249. rate = SAMPLE_RATE_8000;
  250. else if (rate == 11025)
  251. rate = SAMPLE_RATE_11052;
  252. else if (rate == 12000)
  253. rate = SAMPLE_RATE_12000;
  254. else if (rate == 16000)
  255. rate = SAMPLE_RATE_16000;
  256. else if (rate == 22050)
  257. rate = SAMPLE_RATE_22050;
  258. else if (rate == 24000)
  259. rate = SAMPLE_RATE_24000;
  260. else if (rate == 32000)
  261. rate = SAMPLE_RATE_32000;
  262. else if (rate == 44100)
  263. rate = SAMPLE_RATE_44100;
  264. else if (rate == 48000)
  265. rate = SAMPLE_RATE_48000;
  266. else if (rate == 96000)
  267. rate = SAMPLE_RATE_96000;
  268. else if (rate == 192000)
  269. rate = SAMPLE_RATE_192000;
  270. else {
  271. ESP_LOGW(TAG, "Unknown sample rate %hu", rate);
  272. rate = SAMPLE_RATE_44100;
  273. }
  274. i2c_write_reg(I2S_SR_CTRL, rate);
  275. }
  276. /****************************************************************************************
  277. * Get normalized (0..100) speaker volume
  278. */
  279. static int ac101_get_spk_volume(void) {
  280. return ((i2c_read_reg(SPKOUT_CTRL) & 0x1f) * 100) / 0x1f;
  281. }
  282. /****************************************************************************************
  283. * Set normalized (0..100) volume
  284. */
  285. static void ac101_set_spk_volume(uint8_t volume) {
  286. uint16_t value = min(volume, 100);
  287. value = ((int)value * 0x1f) / 100;
  288. value |= i2c_read_reg(SPKOUT_CTRL) & ~0x1f;
  289. i2c_write_reg(SPKOUT_CTRL, value);
  290. }
  291. /****************************************************************************************
  292. * Get normalized (0..100) earphone volume
  293. */
  294. static int ac101_get_earph_volume(void) {
  295. return (((i2c_read_reg(HPOUT_CTRL) >> 4) & 0x3f) * 100) / 0x3f;
  296. }
  297. /****************************************************************************************
  298. * Set normalized (0..100) earphone volume
  299. */
  300. static void ac101_set_earph_volume(uint8_t volume) {
  301. uint16_t value = min(volume, 255);
  302. value = (((int)value * 0x3f) / 255) << 4;
  303. value |= i2c_read_reg(HPOUT_CTRL) & ~(0x3f << 4);
  304. i2c_write_reg(HPOUT_CTRL, value);
  305. }
  306. /****************************************************************************************
  307. *
  308. */
  309. static void ac101_set_output_mixer_gain(ac_output_mixer_gain_t gain,
  310. ac_output_mixer_source_t source) {
  311. uint16_t regval, temp, clrbit;
  312. regval = i2c_read_reg(OMIXER_BST1_CTRL);
  313. switch (source) {
  314. case SRC_MIC1:
  315. temp = (gain & 0x7) << 6;
  316. clrbit = ~(0x7 << 6);
  317. break;
  318. case SRC_MIC2:
  319. temp = (gain & 0x7) << 3;
  320. clrbit = ~(0x7 << 3);
  321. break;
  322. case SRC_LINEIN:
  323. temp = (gain & 0x7);
  324. clrbit = ~0x7;
  325. break;
  326. default:
  327. return;
  328. }
  329. regval &= clrbit;
  330. regval |= temp;
  331. i2c_write_reg(OMIXER_BST1_CTRL, regval);
  332. }
  333. /****************************************************************************************
  334. *
  335. */
  336. static void ac101_start(ac_module_t mode) {
  337. if (mode == AC_MODULE_LINE) {
  338. i2c_write_reg(0x51, 0x0408);
  339. i2c_write_reg(0x40, 0x8000);
  340. i2c_write_reg(0x50, 0x3bc0);
  341. }
  342. if (mode == AC_MODULE_ADC || mode == AC_MODULE_ADC_DAC ||
  343. mode == AC_MODULE_LINE) {
  344. // I2S1_SDOUT_CTRL
  345. // i2c_write_reg(PLL_CTRL2, 0x8120);
  346. i2c_write_reg(0x04, 0x800c);
  347. i2c_write_reg(0x05, 0x800c);
  348. // res |= i2c_write_reg(0x06, 0x3000);
  349. }
  350. if (mode == AC_MODULE_DAC || mode == AC_MODULE_ADC_DAC ||
  351. mode == AC_MODULE_LINE) {
  352. uint16_t value = i2c_read_reg(PLL_CTRL2);
  353. value |= 0x8000;
  354. i2c_write_reg(PLL_CTRL2, value);
  355. }
  356. }
  357. /****************************************************************************************
  358. *
  359. */
  360. static void ac101_stop(void) {
  361. uint16_t value = i2c_read_reg(PLL_CTRL2);
  362. value &= ~0x8000;
  363. i2c_write_reg(PLL_CTRL2, value);
  364. }
  365. /****************************************************************************************
  366. *
  367. */
  368. static void ac101_deinit(void) {
  369. i2c_write_reg(CHIP_AUDIO_RS, 0x123); //soft reset
  370. }
  371. /****************************************************************************************
  372. * Don't know when this one is supposed to be called
  373. */
  374. static void ac101_i2s_config_clock(ac_i2s_clock_t* cfg) {
  375. uint16_t regval = 0;
  376. regval = i2c_read_reg(I2S1LCK_CTRL);
  377. regval &= 0xe03f;
  378. regval |= (cfg->bclk_div << 9);
  379. regval |= (cfg->lclk_div << 6);
  380. i2c_write_reg(I2S1LCK_CTRL, regval);
  381. }