dac_57xx.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. // this is the only hard-wired thing
  29. #define VOLUME_GPIO 14
  30. #define TAS575x 0x98
  31. #define TAS578x 0x90
  32. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config);
  33. static void deinit(void);
  34. static void speaker(bool active);
  35. static void headset(bool active);
  36. static void volume(unsigned left, unsigned right);
  37. static void power(adac_power_e mode);
  38. struct adac_s dac_tas57xx = { init, deinit, power, speaker, headset, volume };
  39. struct tas57xx_cmd_s {
  40. uint8_t reg;
  41. uint8_t value;
  42. };
  43. static const struct tas57xx_cmd_s tas57xx_init_sequence[] = {
  44. { 0x00, 0x00 }, // select page 0
  45. { 0x02, 0x10 }, // standby
  46. { 0x0d, 0x10 }, // use SCK for PLL
  47. { 0x25, 0x08 }, // ignore SCK halt
  48. { 0x08, 0x10 }, // Mute control enable (from TAS5780)
  49. { 0x54, 0x02 }, // Mute output control (from TAS5780)
  50. { 0x02, 0x00 }, // restart
  51. { 0xff, 0xff } // end of table
  52. };
  53. // matching orders
  54. typedef enum { TAS57_ACTIVE = 0, TAS57_STANDBY, TAS57_DOWN, TAS57_ANALOGUE_OFF, TAS57_ANALOGUE_ON, TAS57_VOLUME } dac_cmd_e;
  55. static const struct tas57xx_cmd_s tas57xx_cmd[] = {
  56. { 0x02, 0x00 }, // TAS57_ACTIVE
  57. { 0x02, 0x10 }, // TAS57_STANDBY
  58. { 0x02, 0x01 }, // TAS57_DOWN
  59. { 0x56, 0x10 }, // TAS57_ANALOGUE_OFF
  60. { 0x56, 0x00 }, // TAS57_ANALOGUE_ON
  61. };
  62. static log_level loglevel = lINFO;
  63. static u8_t tas57_addr;
  64. static int i2c_port;
  65. static void dac_cmd(dac_cmd_e cmd, ...);
  66. static int tas57_detect(void);
  67. /****************************************************************************************
  68. * init
  69. */
  70. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *i2s_config) {
  71. i2c_port = i2c_port_num;
  72. // configure i2c
  73. i2c_config_t i2c_config = {
  74. .mode = I2C_MODE_MASTER,
  75. .sda_io_num = 27,
  76. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  77. .scl_io_num = 26,
  78. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  79. .master.clk_speed = 100000,
  80. };
  81. i2c_param_config(i2c_port, &i2c_config);
  82. i2c_driver_install(i2c_port, I2C_MODE_MASTER, false, false, false);
  83. // find which TAS we are using (if any)
  84. tas57_addr = tas57_detect();
  85. if (!tas57_addr) {
  86. LOG_WARN("No TAS57xx detected");
  87. i2c_driver_delete(i2c_port);
  88. return 0;
  89. }
  90. LOG_INFO("TAS57xx DAC using I2C sda:%u, scl:%u", i2c_config.sda_io_num, i2c_config.scl_io_num);
  91. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  92. for (int i = 0; tas57xx_init_sequence[i].reg != 0xff; i++) {
  93. i2c_master_start(i2c_cmd);
  94. i2c_master_write_byte(i2c_cmd, tas57_addr | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  95. i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].reg, I2C_MASTER_NACK);
  96. i2c_master_write_byte(i2c_cmd, tas57xx_init_sequence[i].value, I2C_MASTER_NACK);
  97. LOG_DEBUG("i2c write %x at %u", tas57xx_init_sequence[i].reg, tas57xx_init_sequence[i].value);
  98. }
  99. i2c_master_stop(i2c_cmd);
  100. esp_err_t res = i2c_master_cmd_begin(i2c_port, i2c_cmd, 500 / portTICK_RATE_MS);
  101. i2c_cmd_link_delete(i2c_cmd);
  102. // configure I2S pins & install driver
  103. 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,
  104. .data_out_num = CONFIG_I2S_DO_IO, .data_in_num = CONFIG_I2S_DI_IO,
  105. };
  106. res |= i2s_driver_install(i2s_num, i2s_config, 0, NULL);
  107. res |= i2s_set_pin(i2s_num, &i2s_pin_config);
  108. LOG_INFO("DAC using I2S bck:%d, ws:%d, do:%d", i2s_pin_config.bck_io_num, i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
  109. if (res == ESP_OK) {
  110. // init volume & mute
  111. gpio_pad_select_gpio(VOLUME_GPIO);
  112. gpio_set_direction(VOLUME_GPIO, GPIO_MODE_OUTPUT);
  113. gpio_set_level(VOLUME_GPIO, 0);
  114. return true;
  115. } else {
  116. LOG_ERROR("could not intialize TAS57xx %d", res);
  117. return false;
  118. }
  119. }
  120. /****************************************************************************************
  121. * init
  122. */
  123. static void deinit(void) {
  124. i2c_driver_delete(i2c_port);
  125. }
  126. /****************************************************************************************
  127. * change volume
  128. */
  129. static void volume(unsigned left, unsigned right) {
  130. LOG_INFO("TAS57xx volume (L:%u R:%u)", left, right);
  131. gpio_set_level(VOLUME_GPIO, left || right);
  132. }
  133. /****************************************************************************************
  134. * power
  135. */
  136. static void power(adac_power_e mode) {
  137. switch(mode) {
  138. case ADAC_STANDBY:
  139. dac_cmd(TAS57_STANDBY);
  140. break;
  141. case ADAC_ON:
  142. dac_cmd(TAS57_ACTIVE);
  143. break;
  144. case ADAC_OFF:
  145. dac_cmd(TAS57_DOWN);
  146. break;
  147. default:
  148. LOG_WARN("unknown DAC command");
  149. break;
  150. }
  151. }
  152. /****************************************************************************************
  153. * speaker
  154. */
  155. static void speaker(bool active) {
  156. if (active) dac_cmd(TAS57_ANALOGUE_ON);
  157. else dac_cmd(TAS57_ANALOGUE_OFF);
  158. }
  159. /****************************************************************************************
  160. * headset
  161. */
  162. static void headset(bool active) {
  163. }
  164. /****************************************************************************************
  165. * DAC specific commands
  166. */
  167. void dac_cmd(dac_cmd_e cmd, ...) {
  168. va_list args;
  169. esp_err_t ret = ESP_OK;
  170. va_start(args, cmd);
  171. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  172. switch(cmd) {
  173. case TAS57_VOLUME:
  174. LOG_ERROR("DAC volume not handled yet");
  175. break;
  176. default:
  177. i2c_master_start(i2c_cmd);
  178. i2c_master_write_byte(i2c_cmd, tas57_addr | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  179. i2c_master_write_byte(i2c_cmd, tas57xx_cmd[cmd].reg, I2C_MASTER_NACK);
  180. i2c_master_write_byte(i2c_cmd, tas57xx_cmd[cmd].value, I2C_MASTER_NACK);
  181. i2c_master_stop(i2c_cmd);
  182. ret = i2c_master_cmd_begin(i2c_port, i2c_cmd, 50 / portTICK_RATE_MS);
  183. }
  184. i2c_cmd_link_delete(i2c_cmd);
  185. if (ret != ESP_OK) {
  186. LOG_ERROR("could not intialize TAS57xx %d", ret);
  187. }
  188. va_end(args);
  189. }
  190. /****************************************************************************************
  191. * TAS57 detection
  192. */
  193. static int tas57_detect(void) {
  194. u8_t data, addr[] = {TAS578x, TAS575x};
  195. int ret;
  196. for (int i = 0; i < sizeof(addr); i++) {
  197. i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create();
  198. i2c_master_start(i2c_cmd);
  199. i2c_master_write_byte(i2c_cmd, addr[i] | I2C_MASTER_WRITE, I2C_MASTER_NACK);
  200. i2c_master_write_byte(i2c_cmd, 00, I2C_MASTER_NACK);
  201. i2c_master_start(i2c_cmd);
  202. i2c_master_write_byte(i2c_cmd, addr[i] | I2C_MASTER_READ, I2C_MASTER_NACK);
  203. i2c_master_read_byte(i2c_cmd, &data, I2C_MASTER_NACK);
  204. i2c_master_stop(i2c_cmd);
  205. ret = i2c_master_cmd_begin(i2c_port, i2c_cmd, 50 / portTICK_RATE_MS);
  206. i2c_cmd_link_delete(i2c_cmd);
  207. if (ret == ESP_OK) {
  208. LOG_INFO("Detected TAS @0x%x", addr[i]);
  209. return addr[i];
  210. }
  211. }
  212. return 0;
  213. }