dac_57xx.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. #include <string.h>
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "driver/i2s.h"
  15. #include "driver/i2c.h"
  16. #include "driver/gpio.h"
  17. #include "esp_log.h"
  18. #include "adac.h"
  19. #define TAS575x 0x98
  20. #define TAS578x 0x90
  21. static const char TAG[] = "TAS575x/8x";
  22. static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config);
  23. static void deinit(void);
  24. static void speaker(bool active);
  25. static void headset(bool active);
  26. static bool volume(unsigned left, unsigned right);
  27. static void power(adac_power_e mode);
  28. const struct adac_s dac_tas57xx = { "TAS57xx", init, deinit, power, speaker, headset, volume };
  29. struct tas57xx_cmd_s {
  30. uint8_t reg;
  31. uint8_t value;
  32. };
  33. static const struct tas57xx_cmd_s tas57xx_init_sequence[] = {
  34. { 0x00, 0x00 }, // select page 0
  35. { 0x02, 0x10 }, // standby
  36. { 0x0d, 0x10 }, // use SCK for PLL
  37. { 0x25, 0x08 }, // ignore SCK halt
  38. { 0x08, 0x10 }, // Mute control enable (from TAS5780)
  39. { 0x54, 0x02 }, // Mute output control (from TAS5780)
  40. { 0x02, 0x00 }, // restart
  41. { 0xff, 0xff } // end of table
  42. };
  43. // matching orders
  44. typedef enum { TAS57_ACTIVE = 0, TAS57_STANDBY, TAS57_DOWN, TAS57_ANALOGUE_OFF, TAS57_ANALOGUE_ON, TAS57_VOLUME } dac_cmd_e;
  45. static const struct tas57xx_cmd_s tas57xx_cmd[] = {
  46. { 0x02, 0x00 }, // TAS57_ACTIVE
  47. { 0x02, 0x10 }, // TAS57_STANDBY
  48. { 0x02, 0x01 }, // TAS57_DOWN
  49. { 0x56, 0x10 }, // TAS57_ANALOGUE_OFF
  50. { 0x56, 0x00 }, // TAS57_ANALOGUE_ON
  51. };
  52. static uint8_t tas57_addr;
  53. static int i2c_port;
  54. static void dac_cmd(dac_cmd_e cmd, ...);
  55. static int tas57_detect(void);
  56. /****************************************************************************************
  57. * init
  58. */
  59. static bool init(char *config, int i2c_port_num, i2s_config_t *i2s_config) {
  60. char *p;
  61. i2c_port = i2c_port_num;
  62. // configure i2c
  63. i2c_config_t i2c_config = {
  64. .mode = I2C_MODE_MASTER,
  65. .sda_io_num = -1,
  66. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  67. .scl_io_num = -1,
  68. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  69. .master.clk_speed = 250000,
  70. };
  71. if ((p = strcasestr(config, "sda")) != NULL) i2c_config.sda_io_num = atoi(strchr(p, '=') + 1);
  72. if ((p = strcasestr(config, "scl")) != NULL) i2c_config.scl_io_num = atoi(strchr(p, '=') + 1);
  73. i2c_param_config(i2c_port, &i2c_config);
  74. i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  75. // find which TAS we are using (if any)
  76. tas57_addr = tas57_detect();
  77. if (!tas57_addr) {
  78. ESP_LOGW(TAG, "No TAS57xx detected");
  79. i2c_driver_delete(i2c_port);
  80. return false;
  81. }
  82. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  83. for (int i = 0; tas57xx_init_sequence[i].reg != 0xff; i++) {
  84. i2c_master_start(i2c_cmd);
  85. i2c_master_write_byte(i2c_cmd, tas57_addr | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  86. i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].reg, I2C_MASTER_NACK);
  87. i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].value, I2C_MASTER_NACK);
  88. ESP_LOGD(TAG, "i2c write %x at %u", tas57xx_init_sequence[i].reg, tas57xx_init_sequence[i].value);
  89. }
  90. i2c_master_stop(i2c_cmd);
  91. esp_err_t res = i2c_master_cmd_begin(i2c_port, i2c_cmd, 500 / portTICK_RATE_MS);
  92. i2c_cmd_link_delete(i2c_cmd);
  93. ESP_LOGI(TAG, "TAS57xx uses I2C sda:%d, scl:%d", i2c_config.sda_io_num, i2c_config.scl_io_num);
  94. if (res != ESP_OK) {
  95. ESP_LOGE(TAG, "could not intialize TAS57xx %d", res);
  96. return false;
  97. }
  98. return true;
  99. }
  100. /****************************************************************************************
  101. * init
  102. */
  103. static void deinit(void) {
  104. i2c_driver_delete(i2c_port);
  105. }
  106. /****************************************************************************************
  107. * change volume
  108. */
  109. static bool volume(unsigned left, unsigned right) {
  110. return false;
  111. }
  112. /****************************************************************************************
  113. * power
  114. */
  115. static void power(adac_power_e mode) {
  116. switch(mode) {
  117. case ADAC_STANDBY:
  118. dac_cmd(TAS57_STANDBY);
  119. break;
  120. case ADAC_ON:
  121. dac_cmd(TAS57_ACTIVE);
  122. break;
  123. case ADAC_OFF:
  124. dac_cmd(TAS57_DOWN);
  125. break;
  126. default:
  127. ESP_LOGW(TAG, "unknown DAC command");
  128. break;
  129. }
  130. }
  131. /****************************************************************************************
  132. * speaker
  133. */
  134. static void speaker(bool active) {
  135. if (active) dac_cmd(TAS57_ANALOGUE_ON);
  136. else dac_cmd(TAS57_ANALOGUE_OFF);
  137. }
  138. /****************************************************************************************
  139. * headset
  140. */
  141. static void headset(bool active) { }
  142. /****************************************************************************************
  143. * DAC specific commands
  144. */
  145. void dac_cmd(dac_cmd_e cmd, ...) {
  146. va_list args;
  147. esp_err_t ret = ESP_OK;
  148. va_start(args, cmd);
  149. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  150. switch(cmd) {
  151. case TAS57_VOLUME:
  152. ESP_LOGE(TAG, "DAC volume not handled yet");
  153. break;
  154. default:
  155. i2c_master_start(i2c_cmd);
  156. i2c_master_write_byte(i2c_cmd, tas57_addr | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  157. i2c_master_write_byte(i2c_cmd, tas57xx_cmd[cmd].reg, I2C_MASTER_NACK);
  158. i2c_master_write_byte(i2c_cmd, tas57xx_cmd[cmd].value, I2C_MASTER_NACK);
  159. i2c_master_stop(i2c_cmd);
  160. ret = i2c_master_cmd_begin(i2c_port, i2c_cmd, 50 / portTICK_RATE_MS);
  161. }
  162. i2c_cmd_link_delete(i2c_cmd);
  163. if (ret != ESP_OK) {
  164. ESP_LOGE(TAG, "could not intialize TAS57xx %d", ret);
  165. }
  166. va_end(args);
  167. }
  168. /****************************************************************************************
  169. * TAS57 detection
  170. */
  171. static int tas57_detect(void) {
  172. uint8_t data, addr[] = {TAS578x, TAS575x};
  173. int ret;
  174. for (int i = 0; i < sizeof(addr); i++) {
  175. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  176. i2c_master_start(i2c_cmd);
  177. i2c_master_write_byte(i2c_cmd, addr[i] | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  178. i2c_master_write_byte(i2c_cmd, 00, I2C_MASTER_NACK);
  179. i2c_master_start(i2c_cmd);
  180. i2c_master_write_byte(i2c_cmd, addr[i] | I2C_MASTER_READ, I2C_MASTER_NACK);
  181. i2c_master_read_byte(i2c_cmd, &data, I2C_MASTER_NACK);
  182. i2c_master_stop(i2c_cmd);
  183. ret = i2c_master_cmd_begin(i2c_port, i2c_cmd, 50 / portTICK_RATE_MS);
  184. i2c_cmd_link_delete(i2c_cmd);
  185. if (ret == ESP_OK) {
  186. ESP_LOGI(TAG, "Detected TAS @0x%x", addr[i]);
  187. return addr[i];
  188. }
  189. }
  190. return 0;
  191. }