ac101.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. const static char TAG[] = "AC101";
  35. #define SPKOUT_EN ((1 << 11) | (1 << 7))
  36. #define EAROUT_EN ((1 << 11) | (1 << 12) | (1 << 13))
  37. #define min(a,b) (((a) < (b)) ? (a) : (b))
  38. #define max(a,b) (((a) > (b)) ? (a) : (b))
  39. #define AC_ASSERT(a, format, b, ...) \
  40. if ((a) != 0) { \
  41. ESP_LOGE(TAG, format, ##__VA_ARGS__); \
  42. return b;\
  43. }
  44. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config);
  45. static void deinit(void);
  46. static void speaker(bool active);
  47. static void headset(bool active);
  48. static void volume(unsigned left, unsigned right);
  49. static void power(adac_power_e mode);
  50. struct adac_s dac_a1s = { init, deinit, power, speaker, headset, volume };
  51. static esp_err_t i2c_write_reg(uint8_t reg, uint16_t val);
  52. static uint16_t i2c_read_reg(uint8_t reg);
  53. static void ac101_start(ac_module_t mode);
  54. static void ac101_stop(void);
  55. static void ac101_set_earph_volume(uint8_t volume);
  56. static void ac101_set_spk_volume(uint8_t volume);
  57. static int ac101_get_spk_volume(void);
  58. static int i2c_port;
  59. /****************************************************************************************
  60. * init
  61. */
  62. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
  63. esp_err_t res;
  64. ESP_LOGI(TAG, "Initializing AC101");
  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 = 100000,
  74. };
  75. i2c_param_config(i2c_port, &i2c_config);
  76. i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  77. ESP_LOGI(TAG, "DAC using I2C sda:%u, scl:%u", i2c_config.sda_io_num, i2c_config.scl_io_num);
  78. res = i2c_write_reg(CHIP_AUDIO_RS, 0x123);
  79. //huh?
  80. //vTaskDelay(1000 / portTICK_PERIOD_MS);
  81. if (ESP_OK != res) {
  82. ESP_LOGE(TAG, "AC101 reset failed! %d", res);
  83. return false;
  84. }
  85. // Enable the PLL from 256*44.1KHz MCLK source
  86. i2c_write_reg(PLL_CTRL1, 0x014f);
  87. //res |= i2c_write_reg(PLL_CTRL2, 0x83c0);
  88. i2c_write_reg(PLL_CTRL2, 0x8600);
  89. //Clocking system
  90. i2c_write_reg(SYSCLK_CTRL, 0x8b08);
  91. i2c_write_reg(MOD_CLK_ENA, 0x800c);
  92. i2c_write_reg(MOD_RST_CTRL, 0x800c);
  93. i2c_write_reg(I2S_SR_CTRL, 0x7000); //sample rate
  94. //AIF config
  95. i2c_write_reg(I2S1LCK_CTRL, 0x8850); //BCLK/LRCK
  96. i2c_write_reg(I2S1_SDOUT_CTRL, 0xc000);
  97. i2c_write_reg(I2S1_SDIN_CTRL, 0xc000);
  98. i2c_write_reg(I2S1_MXR_SRC, 0x2200);
  99. i2c_write_reg(ADC_SRCBST_CTRL, 0xccc4);
  100. i2c_write_reg(ADC_SRC, 0x2020);
  101. i2c_write_reg(ADC_DIG_CTRL, 0x8000);
  102. i2c_write_reg(ADC_APC_CTRL, 0xbbc3);
  103. //Path Configuration
  104. i2c_write_reg(DAC_MXR_SRC, 0xcc00);
  105. i2c_write_reg(DAC_DIG_CTRL, 0x8000);
  106. i2c_write_reg(OMIXER_SR, 0x0081);
  107. i2c_write_reg(OMIXER_DACA_CTRL, 0xf080);
  108. // configure I2S
  109. uint16_t regval = i2c_read_reg(I2S1LCK_CTRL);
  110. regval &= 0xffc3;
  111. regval |= (AC_MODE_SLAVE << 15);
  112. regval |= (BIT_LENGTH_16_BITS << 4);
  113. regval |= (AC_MODE_SLAVE << 2);
  114. res |= i2c_write_reg(I2S1LCK_CTRL, regval);
  115. res |= i2c_write_reg(I2S_SR_CTRL, SAMPLE_RATE_44100);
  116. // configure I2S pins & install driver
  117. i2s_pin_config_t i2s_pin_config = (i2s_pin_config_t) { .bck_io_num = 27, .ws_io_num = 26,
  118. .data_out_num = 25, .data_in_num = 35 //Not used
  119. };
  120. i2s_driver_install(i2s_num, i2s_config, 0, NULL);
  121. i2s_set_pin(i2s_num, &i2s_pin_config);
  122. // enable earphone & speaker
  123. i2c_write_reg(SPKOUT_CTRL, 0x0220);
  124. i2c_write_reg(OMIXER_DACA_CTRL, 0xff00);
  125. i2c_write_reg(HPOUT_CTRL, 0x3801);
  126. // set gain for speaker and earphone
  127. ac101_set_spk_volume(100);
  128. ac101_set_earph_volume(100);
  129. ESP_LOGI(TAG, "DAC using I2S bck:%u, ws:%u, do:%u", i2s_pin_config.bck_io_num, i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
  130. return true;
  131. }
  132. /****************************************************************************************
  133. * init
  134. */
  135. static void deinit(void) {
  136. i2c_driver_delete(i2c_port);
  137. }
  138. /****************************************************************************************
  139. * change volume
  140. */
  141. static void volume(unsigned left, unsigned right) {
  142. // nothing at that point, volume is handled by backend
  143. }
  144. /****************************************************************************************
  145. * power
  146. */
  147. static void power(adac_power_e mode) {
  148. switch(mode) {
  149. case ADAC_STANDBY:
  150. case ADAC_OFF:
  151. ac101_stop();
  152. break;
  153. case ADAC_ON:
  154. ac101_start(AC_MODULE_DAC);
  155. break;
  156. default:
  157. ESP_LOGW(TAG, "unknown power command");
  158. break;
  159. }
  160. }
  161. /****************************************************************************************
  162. * speaker
  163. */
  164. static void speaker(bool active) {
  165. uint16_t value = i2c_read_reg(SPKOUT_CTRL);
  166. if (active) i2c_write_reg(SPKOUT_CTRL, value | SPKOUT_EN);
  167. else i2c_write_reg(SPKOUT_CTRL, value & ~SPKOUT_EN);
  168. }
  169. /****************************************************************************************
  170. * headset
  171. */
  172. static void headset(bool active) {
  173. uint16_t value = i2c_read_reg(HPOUT_CTRL);
  174. if (active) i2c_write_reg(HPOUT_CTRL, value | EAROUT_EN);
  175. else i2c_write_reg(HPOUT_CTRL, value & ~EAROUT_EN);
  176. }
  177. /****************************************************************************************
  178. *
  179. */
  180. static esp_err_t i2c_write_reg(uint8_t reg, uint16_t val)
  181. {
  182. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  183. esp_err_t ret =0;
  184. uint8_t send_buff[4];
  185. send_buff[0] = (AC101_ADDR << 1);
  186. send_buff[1] = reg;
  187. send_buff[2] = (val>>8) & 0xff;
  188. send_buff[3] = val & 0xff;
  189. ret |= i2c_master_start(cmd);
  190. ret |= i2c_master_write(cmd, send_buff, 4, ACK_CHECK_EN);
  191. ret |= i2c_master_stop(cmd);
  192. ret |= i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
  193. i2c_cmd_link_delete(cmd);
  194. return ret;
  195. }
  196. /****************************************************************************************
  197. *
  198. */
  199. static uint16_t i2c_read_reg(uint8_t reg) {
  200. uint8_t data[2] = { 0 };
  201. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  202. i2c_master_start(cmd);
  203. i2c_master_write_byte(cmd, ( AC101_ADDR << 1 ) | WRITE_BIT, ACK_CHECK_EN);
  204. i2c_master_write_byte(cmd, reg, ACK_CHECK_EN);
  205. i2c_master_start(cmd);
  206. i2c_master_write_byte(cmd, ( AC101_ADDR << 1 ) | READ_BIT, ACK_CHECK_EN); //check or not
  207. i2c_master_read(cmd, data, 2, ACK_VAL);
  208. i2c_master_stop(cmd);
  209. i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
  210. i2c_cmd_link_delete(cmd);
  211. return (data[0] << 8) + data[1];;
  212. }
  213. /****************************************************************************************
  214. *
  215. */
  216. void set_sample_rate(int rate) {
  217. if (rate == 8000) rate = SAMPLE_RATE_8000;
  218. else if (rate == 11025) rate = SAMPLE_RATE_11052;
  219. else if (rate == 12000) rate = SAMPLE_RATE_12000;
  220. else if (rate == 16000) rate = SAMPLE_RATE_16000;
  221. else if (rate == 22050) rate = SAMPLE_RATE_22050;
  222. else if (rate == 24000) rate = SAMPLE_RATE_24000;
  223. else if (rate == 32000) rate = SAMPLE_RATE_32000;
  224. else if (rate == 44100) rate = SAMPLE_RATE_44100;
  225. else if (rate == 48000) rate = SAMPLE_RATE_48000;
  226. else if (rate == 96000) rate = SAMPLE_RATE_96000;
  227. else if (rate == 192000) rate = SAMPLE_RATE_192000;
  228. else {
  229. ESP_LOGW(TAG, "Unknown sample rate %hu", rate);
  230. rate = SAMPLE_RATE_44100;
  231. }
  232. i2c_write_reg(I2S_SR_CTRL, rate);
  233. }
  234. /****************************************************************************************
  235. * Get normalized (0..100) speaker volume
  236. */
  237. static int ac101_get_spk_volume(void) {
  238. return ((i2c_read_reg(SPKOUT_CTRL) & 0x1f) * 100) / 0x1f;
  239. }
  240. /****************************************************************************************
  241. * Set normalized (0..100) volume
  242. */
  243. static void ac101_set_spk_volume(uint8_t volume) {
  244. volume = max(volume, 0x1f);
  245. volume = ((int) volume * 0x1f) / 100;
  246. volume |= i2c_read_reg(SPKOUT_CTRL) & ~0x1f;
  247. i2c_write_reg(SPKOUT_CTRL, volume);
  248. }
  249. /****************************************************************************************
  250. * Get normalized (0..100) earphonz volume
  251. */
  252. static int ac101_get_earph_volume(void) {
  253. return (((i2c_read_reg(HPOUT_CTRL) >> 4) & 0x3f) * 100) / 0x3f;
  254. }
  255. /****************************************************************************************
  256. * Set normalized (0..100) earphone volume
  257. */
  258. static void ac101_set_earph_volume(uint8_t volume) {
  259. volume = max(volume, 0x3f);
  260. volume = (((int) volume * 0x3f) / 100) << 4;
  261. volume |= i2c_read_reg(HPOUT_CTRL) & ~(0x3f << 4);
  262. i2c_write_reg(HPOUT_CTRL, volume);
  263. }
  264. /****************************************************************************************
  265. *
  266. */
  267. static void ac101_set_output_mixer_gain(ac_output_mixer_gain_t gain,ac_output_mixer_source_t source)
  268. {
  269. uint16_t regval,temp,clrbit;
  270. regval = i2c_read_reg(OMIXER_BST1_CTRL);
  271. switch(source){
  272. case SRC_MIC1:
  273. temp = (gain&0x7) << 6;
  274. clrbit = ~(0x7<<6);
  275. break;
  276. case SRC_MIC2:
  277. temp = (gain&0x7) << 3;
  278. clrbit = ~(0x7<<3);
  279. break;
  280. case SRC_LINEIN:
  281. temp = (gain&0x7);
  282. clrbit = ~0x7;
  283. break;
  284. default:
  285. return;
  286. }
  287. regval &= clrbit;
  288. regval |= temp;
  289. i2c_write_reg(OMIXER_BST1_CTRL,regval);
  290. }
  291. /****************************************************************************************
  292. *
  293. */
  294. static void ac101_start(ac_module_t mode) {
  295. if (mode == AC_MODULE_LINE) {
  296. i2c_write_reg(0x51, 0x0408);
  297. i2c_write_reg(0x40, 0x8000);
  298. i2c_write_reg(0x50, 0x3bc0);
  299. }
  300. if (mode == AC_MODULE_ADC || mode == AC_MODULE_ADC_DAC || mode == AC_MODULE_LINE) {
  301. //I2S1_SDOUT_CTRL
  302. //i2c_write_reg(PLL_CTRL2, 0x8120);
  303. i2c_write_reg(0x04, 0x800c);
  304. i2c_write_reg(0x05, 0x800c);
  305. //res |= i2c_write_reg(0x06, 0x3000);
  306. }
  307. if (mode == AC_MODULE_DAC || mode == AC_MODULE_ADC_DAC || mode == AC_MODULE_LINE) {
  308. headset(true);
  309. speaker(true);
  310. ac101_set_earph_volume(100);
  311. ac101_set_spk_volume(100);
  312. }
  313. }
  314. /****************************************************************************************
  315. *
  316. */
  317. static void ac101_stop(void) {
  318. speaker(false);
  319. headset(false);
  320. }
  321. /****************************************************************************************
  322. *
  323. */
  324. static void ac101_deinit(void) {
  325. i2c_write_reg(CHIP_AUDIO_RS, 0x123); //soft reset
  326. }
  327. /****************************************************************************************
  328. * Don't know when this one is supposed to be called
  329. */
  330. static void ac101_i2s_config_clock(ac_i2s_clock_t *cfg) {
  331. uint16_t regval=0;
  332. regval = i2c_read_reg(I2S1LCK_CTRL);
  333. regval &= 0xe03f;
  334. regval |= (cfg->bclk_div << 9);
  335. regval |= (cfg->lclk_div << 6);
  336. i2c_write_reg(I2S1LCK_CTRL, regval);
  337. }