layla20_dsp.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /****************************************************************************
  2. Copyright Echo Digital Audio Corporation (c) 1998 - 2004
  3. All rights reserved
  4. www.echoaudio.com
  5. This file is part of Echo Digital Audio's generic driver library.
  6. Echo Digital Audio's generic driver library is free software;
  7. you can redistribute it and/or modify it under the terms of
  8. the GNU General Public License as published by the Free Software
  9. Foundation.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  17. MA 02111-1307, USA.
  18. *************************************************************************
  19. Translation from C++ and adaptation for use in ALSA-Driver
  20. were made by Giuliano Pochini <pochini@shiny.it>
  21. ****************************************************************************/
  22. static int read_dsp(struct echoaudio *chip, u32 *data);
  23. static int set_professional_spdif(struct echoaudio *chip, char prof);
  24. static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic);
  25. static int check_asic_status(struct echoaudio *chip);
  26. static int update_flags(struct echoaudio *chip);
  27. static int init_hw(struct echoaudio *chip, u16 device_id, u16 subdevice_id)
  28. {
  29. int err;
  30. if (snd_BUG_ON((subdevice_id & 0xfff0) != LAYLA20))
  31. return -ENODEV;
  32. if ((err = init_dsp_comm_page(chip))) {
  33. dev_err(chip->card->dev,
  34. "init_hw - could not initialize DSP comm page\n");
  35. return err;
  36. }
  37. chip->device_id = device_id;
  38. chip->subdevice_id = subdevice_id;
  39. chip->bad_board = true;
  40. chip->has_midi = true;
  41. chip->dsp_code_to_load = FW_LAYLA20_DSP;
  42. chip->input_clock_types =
  43. ECHO_CLOCK_BIT_INTERNAL | ECHO_CLOCK_BIT_SPDIF |
  44. ECHO_CLOCK_BIT_WORD | ECHO_CLOCK_BIT_SUPER;
  45. chip->output_clock_types =
  46. ECHO_CLOCK_BIT_WORD | ECHO_CLOCK_BIT_SUPER;
  47. if ((err = load_firmware(chip)) < 0)
  48. return err;
  49. chip->bad_board = false;
  50. return err;
  51. }
  52. static int set_mixer_defaults(struct echoaudio *chip)
  53. {
  54. chip->professional_spdif = false;
  55. return init_line_levels(chip);
  56. }
  57. static u32 detect_input_clocks(const struct echoaudio *chip)
  58. {
  59. u32 clocks_from_dsp, clock_bits;
  60. /* Map the DSP clock detect bits to the generic driver clock detect bits */
  61. clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
  62. clock_bits = ECHO_CLOCK_BIT_INTERNAL;
  63. if (clocks_from_dsp & GLDM_CLOCK_DETECT_BIT_SPDIF)
  64. clock_bits |= ECHO_CLOCK_BIT_SPDIF;
  65. if (clocks_from_dsp & GLDM_CLOCK_DETECT_BIT_WORD) {
  66. if (clocks_from_dsp & GLDM_CLOCK_DETECT_BIT_SUPER)
  67. clock_bits |= ECHO_CLOCK_BIT_SUPER;
  68. else
  69. clock_bits |= ECHO_CLOCK_BIT_WORD;
  70. }
  71. return clock_bits;
  72. }
  73. /* ASIC status check - some cards have one or two ASICs that need to be
  74. loaded. Once that load is complete, this function is called to see if
  75. the load was successful.
  76. If this load fails, it does not necessarily mean that the hardware is
  77. defective - the external box may be disconnected or turned off.
  78. This routine sometimes fails for Layla20; for Layla20, the loop runs
  79. 5 times and succeeds if it wins on three of the loops. */
  80. static int check_asic_status(struct echoaudio *chip)
  81. {
  82. u32 asic_status;
  83. int goodcnt, i;
  84. chip->asic_loaded = false;
  85. for (i = goodcnt = 0; i < 5; i++) {
  86. send_vector(chip, DSP_VC_TEST_ASIC);
  87. /* The DSP will return a value to indicate whether or not
  88. the ASIC is currently loaded */
  89. if (read_dsp(chip, &asic_status) < 0) {
  90. dev_err(chip->card->dev,
  91. "check_asic_status: failed on read_dsp\n");
  92. return -EIO;
  93. }
  94. if (asic_status == ASIC_ALREADY_LOADED) {
  95. if (++goodcnt == 3) {
  96. chip->asic_loaded = true;
  97. return 0;
  98. }
  99. }
  100. }
  101. return -EIO;
  102. }
  103. /* Layla20 has an ASIC in the external box */
  104. static int load_asic(struct echoaudio *chip)
  105. {
  106. int err;
  107. if (chip->asic_loaded)
  108. return 0;
  109. err = load_asic_generic(chip, DSP_FNC_LOAD_LAYLA_ASIC,
  110. FW_LAYLA20_ASIC);
  111. if (err < 0)
  112. return err;
  113. /* Check if ASIC is alive and well. */
  114. return check_asic_status(chip);
  115. }
  116. static int set_sample_rate(struct echoaudio *chip, u32 rate)
  117. {
  118. if (snd_BUG_ON(rate < 8000 || rate > 50000))
  119. return -EINVAL;
  120. /* Only set the clock for internal mode. Do not return failure,
  121. simply treat it as a non-event. */
  122. if (chip->input_clock != ECHO_CLOCK_INTERNAL) {
  123. dev_warn(chip->card->dev,
  124. "Cannot set sample rate - clock not set to CLK_CLOCKININTERNAL\n");
  125. chip->comm_page->sample_rate = cpu_to_le32(rate);
  126. chip->sample_rate = rate;
  127. return 0;
  128. }
  129. if (wait_handshake(chip))
  130. return -EIO;
  131. dev_dbg(chip->card->dev, "set_sample_rate(%d)\n", rate);
  132. chip->sample_rate = rate;
  133. chip->comm_page->sample_rate = cpu_to_le32(rate);
  134. clear_handshake(chip);
  135. return send_vector(chip, DSP_VC_SET_LAYLA_SAMPLE_RATE);
  136. }
  137. static int set_input_clock(struct echoaudio *chip, u16 clock_source)
  138. {
  139. u16 clock;
  140. u32 rate;
  141. rate = 0;
  142. switch (clock_source) {
  143. case ECHO_CLOCK_INTERNAL:
  144. rate = chip->sample_rate;
  145. clock = LAYLA20_CLOCK_INTERNAL;
  146. break;
  147. case ECHO_CLOCK_SPDIF:
  148. clock = LAYLA20_CLOCK_SPDIF;
  149. break;
  150. case ECHO_CLOCK_WORD:
  151. clock = LAYLA20_CLOCK_WORD;
  152. break;
  153. case ECHO_CLOCK_SUPER:
  154. clock = LAYLA20_CLOCK_SUPER;
  155. break;
  156. default:
  157. dev_err(chip->card->dev,
  158. "Input clock 0x%x not supported for Layla24\n",
  159. clock_source);
  160. return -EINVAL;
  161. }
  162. chip->input_clock = clock_source;
  163. chip->comm_page->input_clock = cpu_to_le16(clock);
  164. clear_handshake(chip);
  165. send_vector(chip, DSP_VC_UPDATE_CLOCKS);
  166. if (rate)
  167. set_sample_rate(chip, rate);
  168. return 0;
  169. }
  170. static int set_output_clock(struct echoaudio *chip, u16 clock)
  171. {
  172. switch (clock) {
  173. case ECHO_CLOCK_SUPER:
  174. clock = LAYLA20_OUTPUT_CLOCK_SUPER;
  175. break;
  176. case ECHO_CLOCK_WORD:
  177. clock = LAYLA20_OUTPUT_CLOCK_WORD;
  178. break;
  179. default:
  180. dev_err(chip->card->dev, "set_output_clock wrong clock\n");
  181. return -EINVAL;
  182. }
  183. if (wait_handshake(chip))
  184. return -EIO;
  185. chip->comm_page->output_clock = cpu_to_le16(clock);
  186. chip->output_clock = clock;
  187. clear_handshake(chip);
  188. return send_vector(chip, DSP_VC_UPDATE_CLOCKS);
  189. }
  190. /* Set input bus gain (one unit is 0.5dB !) */
  191. static int set_input_gain(struct echoaudio *chip, u16 input, int gain)
  192. {
  193. if (snd_BUG_ON(input >= num_busses_in(chip)))
  194. return -EINVAL;
  195. if (wait_handshake(chip))
  196. return -EIO;
  197. chip->input_gain[input] = gain;
  198. gain += GL20_INPUT_GAIN_MAGIC_NUMBER;
  199. chip->comm_page->line_in_level[input] = gain;
  200. return 0;
  201. }
  202. /* Tell the DSP to reread the flags from the comm page */
  203. static int update_flags(struct echoaudio *chip)
  204. {
  205. if (wait_handshake(chip))
  206. return -EIO;
  207. clear_handshake(chip);
  208. return send_vector(chip, DSP_VC_UPDATE_FLAGS);
  209. }
  210. static int set_professional_spdif(struct echoaudio *chip, char prof)
  211. {
  212. if (prof)
  213. chip->comm_page->flags |=
  214. cpu_to_le32(DSP_FLAG_PROFESSIONAL_SPDIF);
  215. else
  216. chip->comm_page->flags &=
  217. ~cpu_to_le32(DSP_FLAG_PROFESSIONAL_SPDIF);
  218. chip->professional_spdif = prof;
  219. return update_flags(chip);
  220. }