dac_57xx.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "squeezelite.h"
  22. #include "freertos/FreeRTOS.h"
  23. #include "freertos/task.h"
  24. #include "driver/i2s.h"
  25. #include "driver/i2c.h"
  26. #include "driver/gpio.h"
  27. #include "adac.h"
  28. #define VOLUME_GPIO 14
  29. #define TAS575x 0x98
  30. #define TAS578x 0x90
  31. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config);
  32. static void deinit(void);
  33. static void speaker(bool active);
  34. static void headset(bool active);
  35. static void volume(unsigned left, unsigned right);
  36. static void power(adac_power_e mode);
  37. struct adac_s dac_tas57xx = { init, deinit, power, speaker, headset, volume };
  38. struct tas57xx_cmd_s {
  39. uint8_t reg;
  40. uint8_t value;
  41. };
  42. static const struct tas57xx_cmd_s tas57xx_init_sequence[] = {
  43. { 0x00, 0x00 }, // select page 0
  44. { 0x02, 0x10 }, // standby
  45. { 0x0d, 0x10 }, // use SCK for PLL
  46. { 0x25, 0x08 }, // ignore SCK halt
  47. { 0x08, 0x10 }, // Mute control enable (from TAS5780)
  48. { 0x54, 0x02 }, // Mute output control (from TAS5780)
  49. { 0x02, 0x00 }, // restart
  50. { 0xff, 0xff } // end of table
  51. };
  52. // matching orders
  53. typedef enum { TAS57_ACTIVE = 0, TAS57_STANDBY, TAS57_DOWN, TAS57_ANALOGUE_OFF, TAS57_ANALOGUE_ON, TAS57_VOLUME } dac_cmd_e;
  54. static const struct tas57xx_cmd_s tas57xx_cmd[] = {
  55. { 0x02, 0x00 }, // TAS57_ACTIVE
  56. { 0x02, 0x10 }, // TAS57_STANDBY
  57. { 0x02, 0x01 }, // TAS57_DOWN
  58. { 0x56, 0x10 }, // TAS57_ANALOGUE_OFF
  59. { 0x56, 0x00 }, // TAS57_ANALOGUE_ON
  60. };
  61. static log_level loglevel = lINFO;
  62. static u8_t tas57_addr;
  63. static int i2c_port;
  64. static void dac_cmd(dac_cmd_e cmd, ...);
  65. static int tas57_detect(void);
  66. /****************************************************************************************
  67. * init
  68. */
  69. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
  70. i2c_port = i2c_port_num;
  71. // configure i2c
  72. i2c_config_t i2c_config = {
  73. .mode = I2C_MODE_MASTER,
  74. .sda_io_num = 27,
  75. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  76. .scl_io_num = 26,
  77. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  78. .master.clk_speed = 100000,
  79. };
  80. i2c_param_config(i2c_port, &i2c_config);
  81. i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  82. // find which TAS we are using (if any)
  83. tas57_addr = tas57_detect();
  84. if (!tas57_addr) {
  85. LOG_WARN("No TAS57xx detected");
  86. i2c_driver_delete(i2c_port);
  87. return 0;
  88. }
  89. LOG_INFO("TAS57xx DAC using I2C sda:%u, scl:%u", i2c_config.sda_io_num, i2c_config.scl_io_num);
  90. // init volume & mute
  91. gpio_pad_select_gpio(VOLUME_GPIO);
  92. gpio_set_direction(VOLUME_GPIO, GPIO_MODE_OUTPUT);
  93. gpio_set_level(VOLUME_GPIO, 0);
  94. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  95. for (int i = 0; tas57xx_init_sequence[i].reg != 0xff; i++) {
  96. i2c_master_start(i2c_cmd);
  97. i2c_master_write_byte(i2c_cmd, tas57_addr | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  98. i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].reg, I2C_MASTER_NACK);
  99. i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].value, I2C_MASTER_NACK);
  100. LOG_DEBUG("i2c write %x at %u", tas57xx_init_sequence[i].reg, tas57xx_init_sequence[i].value);
  101. }
  102. i2c_master_stop(i2c_cmd);
  103. esp_err_t ret = i2c_master_cmd_begin(i2c_port, i2c_cmd, 500 / portTICK_RATE_MS);
  104. i2c_cmd_link_delete(i2c_cmd);
  105. // configure I2S pins & install driver
  106. i2s_pin_config_t i2s_pin_config = (i2s_pin_config_t) { .bck_io_num = CONFIG_I2S_BCK_IO, .ws_io_num = CONFIG_I2S_WS_IO,
  107. .data_out_num = CONFIG_I2S_DO_IO, .data_in_num = CONFIG_I2S_DI_IO,
  108. };
  109. i2s_driver_install(i2s_num, i2s_config, 0, NULL);
  110. i2s_set_pin(i2s_num, &i2s_pin_config);
  111. LOG_INFO("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);
  112. if (ret != ESP_OK) {
  113. LOG_ERROR("could not intialize TAS57xx %d", ret);
  114. return false;
  115. } else {
  116. return true;
  117. }
  118. }
  119. /****************************************************************************************
  120. * init
  121. */
  122. static void deinit(void) {
  123. i2c_driver_delete(i2c_port);
  124. }
  125. /****************************************************************************************
  126. * change volume
  127. */
  128. static void volume(unsigned left, unsigned right) {
  129. LOG_INFO("TAS57xx volume (L:%u R:%u)", left, right);
  130. gpio_set_level(VOLUME_GPIO, left || right);
  131. }
  132. /****************************************************************************************
  133. * power
  134. */
  135. static void power(adac_power_e mode) {
  136. switch(mode) {
  137. case ADAC_STANDBY:
  138. dac_cmd(TAS57_STANDBY);
  139. break;
  140. case ADAC_ON:
  141. dac_cmd(TAS57_ACTIVE);
  142. break;
  143. case ADAC_OFF:
  144. dac_cmd(TAS57_DOWN);
  145. break;
  146. default:
  147. LOG_WARN("unknown DAC command");
  148. break;
  149. }
  150. }
  151. /****************************************************************************************
  152. * speaker
  153. */
  154. static void speaker(bool active) {
  155. if (active) dac_cmd(TAS57_ANALOGUE_ON);
  156. else dac_cmd(TAS57_ANALOGUE_OFF);
  157. }
  158. /****************************************************************************************
  159. * headset
  160. */
  161. static void headset(bool active) {
  162. }
  163. /****************************************************************************************
  164. * DAC specific commands
  165. */
  166. void dac_cmd(dac_cmd_e cmd, ...) {
  167. va_list args;
  168. esp_err_t ret = ESP_OK;
  169. va_start(args, cmd);
  170. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  171. switch(cmd) {
  172. case TAS57_VOLUME:
  173. LOG_ERROR("DAC volume not handled yet");
  174. break;
  175. default:
  176. i2c_master_start(i2c_cmd);
  177. i2c_master_write_byte(i2c_cmd, tas57_addr | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  178. i2c_master_write_byte(i2c_cmd, tas57xx_cmd[cmd].reg, I2C_MASTER_NACK);
  179. i2c_master_write_byte(i2c_cmd, tas57xx_cmd[cmd].value, I2C_MASTER_NACK);
  180. i2c_master_stop(i2c_cmd);
  181. ret = i2c_master_cmd_begin(i2c_port, i2c_cmd, 50 / portTICK_RATE_MS);
  182. }
  183. i2c_cmd_link_delete(i2c_cmd);
  184. if (ret != ESP_OK) {
  185. LOG_ERROR("could not intialize TAS57xx %d", ret);
  186. }
  187. va_end(args);
  188. }
  189. /****************************************************************************************
  190. * TAS57 detection
  191. */
  192. static int tas57_detect(void) {
  193. u8_t data, addr[] = {TAS578x, TAS575x};
  194. int ret;
  195. for (int i = 0; i < sizeof(addr); i++) {
  196. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  197. i2c_master_start(i2c_cmd);
  198. i2c_master_write_byte(i2c_cmd, addr[i] | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  199. i2c_master_write_byte(i2c_cmd, 00, I2C_MASTER_NACK);
  200. i2c_master_start(i2c_cmd);
  201. i2c_master_write_byte(i2c_cmd, addr[i] | I2C_MASTER_READ, I2C_MASTER_NACK);
  202. i2c_master_read_byte(i2c_cmd, &data, I2C_MASTER_NACK);
  203. i2c_master_stop(i2c_cmd);
  204. ret = i2c_master_cmd_begin(i2c_port, i2c_cmd, 50 / portTICK_RATE_MS);
  205. i2c_cmd_link_delete(i2c_cmd);
  206. if (ret == ESP_OK) {
  207. LOG_INFO("Detected TAS @0x%x", addr[i]);
  208. return addr[i];
  209. }
  210. }
  211. return 0;
  212. }