gina24_dsp.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 write_control_reg(struct echoaudio *chip, u32 value, char force);
  23. static int set_input_clock(struct echoaudio *chip, u16 clock);
  24. static int set_professional_spdif(struct echoaudio *chip, char prof);
  25. static int set_digital_mode(struct echoaudio *chip, u8 mode);
  26. static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic);
  27. static int check_asic_status(struct echoaudio *chip);
  28. static int init_hw(struct echoaudio *chip, u16 device_id, u16 subdevice_id)
  29. {
  30. int err;
  31. DE_INIT(("init_hw() - Gina24\n"));
  32. if (snd_BUG_ON((subdevice_id & 0xfff0) != GINA24))
  33. return -ENODEV;
  34. if ((err = init_dsp_comm_page(chip))) {
  35. DE_INIT(("init_hw - could not initialize DSP comm page\n"));
  36. return err;
  37. }
  38. chip->device_id = device_id;
  39. chip->subdevice_id = subdevice_id;
  40. chip->bad_board = TRUE;
  41. chip->input_clock_types =
  42. ECHO_CLOCK_BIT_INTERNAL | ECHO_CLOCK_BIT_SPDIF |
  43. ECHO_CLOCK_BIT_ESYNC | ECHO_CLOCK_BIT_ESYNC96 |
  44. ECHO_CLOCK_BIT_ADAT;
  45. /* Gina24 comes in both '301 and '361 flavors */
  46. if (chip->device_id == DEVICE_ID_56361) {
  47. chip->dsp_code_to_load = FW_GINA24_361_DSP;
  48. chip->digital_modes =
  49. ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_RCA |
  50. ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_OPTICAL |
  51. ECHOCAPS_HAS_DIGITAL_MODE_ADAT;
  52. } else {
  53. chip->dsp_code_to_load = FW_GINA24_301_DSP;
  54. chip->digital_modes =
  55. ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_RCA |
  56. ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_OPTICAL |
  57. ECHOCAPS_HAS_DIGITAL_MODE_ADAT |
  58. ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_CDROM;
  59. }
  60. if ((err = load_firmware(chip)) < 0)
  61. return err;
  62. chip->bad_board = FALSE;
  63. DE_INIT(("init_hw done\n"));
  64. return err;
  65. }
  66. static int set_mixer_defaults(struct echoaudio *chip)
  67. {
  68. chip->digital_mode = DIGITAL_MODE_SPDIF_RCA;
  69. chip->professional_spdif = FALSE;
  70. chip->digital_in_automute = TRUE;
  71. return init_line_levels(chip);
  72. }
  73. static u32 detect_input_clocks(const struct echoaudio *chip)
  74. {
  75. u32 clocks_from_dsp, clock_bits;
  76. /* Map the DSP clock detect bits to the generic driver clock
  77. detect bits */
  78. clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
  79. clock_bits = ECHO_CLOCK_BIT_INTERNAL;
  80. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_SPDIF)
  81. clock_bits |= ECHO_CLOCK_BIT_SPDIF;
  82. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_ADAT)
  83. clock_bits |= ECHO_CLOCK_BIT_ADAT;
  84. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_ESYNC)
  85. clock_bits |= ECHO_CLOCK_BIT_ESYNC | ECHO_CLOCK_BIT_ESYNC96;
  86. return clock_bits;
  87. }
  88. /* Gina24 has an ASIC on the PCI card which must be loaded for anything
  89. interesting to happen. */
  90. static int load_asic(struct echoaudio *chip)
  91. {
  92. u32 control_reg;
  93. int err;
  94. short asic;
  95. if (chip->asic_loaded)
  96. return 1;
  97. /* Give the DSP a few milliseconds to settle down */
  98. mdelay(10);
  99. /* Pick the correct ASIC for '301 or '361 Gina24 */
  100. if (chip->device_id == DEVICE_ID_56361)
  101. asic = FW_GINA24_361_ASIC;
  102. else
  103. asic = FW_GINA24_301_ASIC;
  104. err = load_asic_generic(chip, DSP_FNC_LOAD_GINA24_ASIC, asic);
  105. if (err < 0)
  106. return err;
  107. chip->asic_code = asic;
  108. /* Now give the new ASIC a little time to set up */
  109. mdelay(10);
  110. /* See if it worked */
  111. err = check_asic_status(chip);
  112. /* Set up the control register if the load succeeded -
  113. 48 kHz, internal clock, S/PDIF RCA mode */
  114. if (!err) {
  115. control_reg = GML_CONVERTER_ENABLE | GML_48KHZ;
  116. err = write_control_reg(chip, control_reg, TRUE);
  117. }
  118. DE_INIT(("load_asic() done\n"));
  119. return err;
  120. }
  121. static int set_sample_rate(struct echoaudio *chip, u32 rate)
  122. {
  123. u32 control_reg, clock;
  124. if (snd_BUG_ON(rate >= 50000 &&
  125. chip->digital_mode == DIGITAL_MODE_ADAT))
  126. return -EINVAL;
  127. /* Only set the clock for internal mode. */
  128. if (chip->input_clock != ECHO_CLOCK_INTERNAL) {
  129. DE_ACT(("set_sample_rate: Cannot set sample rate - "
  130. "clock not set to CLK_CLOCKININTERNAL\n"));
  131. /* Save the rate anyhow */
  132. chip->comm_page->sample_rate = cpu_to_le32(rate);
  133. chip->sample_rate = rate;
  134. return 0;
  135. }
  136. clock = 0;
  137. control_reg = le32_to_cpu(chip->comm_page->control_register);
  138. control_reg &= GML_CLOCK_CLEAR_MASK & GML_SPDIF_RATE_CLEAR_MASK;
  139. switch (rate) {
  140. case 96000:
  141. clock = GML_96KHZ;
  142. break;
  143. case 88200:
  144. clock = GML_88KHZ;
  145. break;
  146. case 48000:
  147. clock = GML_48KHZ | GML_SPDIF_SAMPLE_RATE1;
  148. break;
  149. case 44100:
  150. clock = GML_44KHZ;
  151. /* Professional mode ? */
  152. if (control_reg & GML_SPDIF_PRO_MODE)
  153. clock |= GML_SPDIF_SAMPLE_RATE0;
  154. break;
  155. case 32000:
  156. clock = GML_32KHZ | GML_SPDIF_SAMPLE_RATE0 |
  157. GML_SPDIF_SAMPLE_RATE1;
  158. break;
  159. case 22050:
  160. clock = GML_22KHZ;
  161. break;
  162. case 16000:
  163. clock = GML_16KHZ;
  164. break;
  165. case 11025:
  166. clock = GML_11KHZ;
  167. break;
  168. case 8000:
  169. clock = GML_8KHZ;
  170. break;
  171. default:
  172. DE_ACT(("set_sample_rate: %d invalid!\n", rate));
  173. return -EINVAL;
  174. }
  175. control_reg |= clock;
  176. chip->comm_page->sample_rate = cpu_to_le32(rate); /* ignored by the DSP */
  177. chip->sample_rate = rate;
  178. DE_ACT(("set_sample_rate: %d clock %d\n", rate, clock));
  179. return write_control_reg(chip, control_reg, FALSE);
  180. }
  181. static int set_input_clock(struct echoaudio *chip, u16 clock)
  182. {
  183. u32 control_reg, clocks_from_dsp;
  184. DE_ACT(("set_input_clock:\n"));
  185. /* Mask off the clock select bits */
  186. control_reg = le32_to_cpu(chip->comm_page->control_register) &
  187. GML_CLOCK_CLEAR_MASK;
  188. clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
  189. switch (clock) {
  190. case ECHO_CLOCK_INTERNAL:
  191. DE_ACT(("Set Gina24 clock to INTERNAL\n"));
  192. chip->input_clock = ECHO_CLOCK_INTERNAL;
  193. return set_sample_rate(chip, chip->sample_rate);
  194. case ECHO_CLOCK_SPDIF:
  195. if (chip->digital_mode == DIGITAL_MODE_ADAT)
  196. return -EAGAIN;
  197. DE_ACT(("Set Gina24 clock to SPDIF\n"));
  198. control_reg |= GML_SPDIF_CLOCK;
  199. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_SPDIF96)
  200. control_reg |= GML_DOUBLE_SPEED_MODE;
  201. else
  202. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  203. break;
  204. case ECHO_CLOCK_ADAT:
  205. if (chip->digital_mode != DIGITAL_MODE_ADAT)
  206. return -EAGAIN;
  207. DE_ACT(("Set Gina24 clock to ADAT\n"));
  208. control_reg |= GML_ADAT_CLOCK;
  209. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  210. break;
  211. case ECHO_CLOCK_ESYNC:
  212. DE_ACT(("Set Gina24 clock to ESYNC\n"));
  213. control_reg |= GML_ESYNC_CLOCK;
  214. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  215. break;
  216. case ECHO_CLOCK_ESYNC96:
  217. DE_ACT(("Set Gina24 clock to ESYNC96\n"));
  218. control_reg |= GML_ESYNC_CLOCK | GML_DOUBLE_SPEED_MODE;
  219. break;
  220. default:
  221. DE_ACT(("Input clock 0x%x not supported for Gina24\n", clock));
  222. return -EINVAL;
  223. }
  224. chip->input_clock = clock;
  225. return write_control_reg(chip, control_reg, TRUE);
  226. }
  227. static int dsp_set_digital_mode(struct echoaudio *chip, u8 mode)
  228. {
  229. u32 control_reg;
  230. int err, incompatible_clock;
  231. /* Set clock to "internal" if it's not compatible with the new mode */
  232. incompatible_clock = FALSE;
  233. switch (mode) {
  234. case DIGITAL_MODE_SPDIF_OPTICAL:
  235. case DIGITAL_MODE_SPDIF_CDROM:
  236. case DIGITAL_MODE_SPDIF_RCA:
  237. if (chip->input_clock == ECHO_CLOCK_ADAT)
  238. incompatible_clock = TRUE;
  239. break;
  240. case DIGITAL_MODE_ADAT:
  241. if (chip->input_clock == ECHO_CLOCK_SPDIF)
  242. incompatible_clock = TRUE;
  243. break;
  244. default:
  245. DE_ACT(("Digital mode not supported: %d\n", mode));
  246. return -EINVAL;
  247. }
  248. spin_lock_irq(&chip->lock);
  249. if (incompatible_clock) { /* Switch to 48KHz, internal */
  250. chip->sample_rate = 48000;
  251. set_input_clock(chip, ECHO_CLOCK_INTERNAL);
  252. }
  253. /* Clear the current digital mode */
  254. control_reg = le32_to_cpu(chip->comm_page->control_register);
  255. control_reg &= GML_DIGITAL_MODE_CLEAR_MASK;
  256. /* Tweak the control reg */
  257. switch (mode) {
  258. case DIGITAL_MODE_SPDIF_OPTICAL:
  259. control_reg |= GML_SPDIF_OPTICAL_MODE;
  260. break;
  261. case DIGITAL_MODE_SPDIF_CDROM:
  262. /* '361 Gina24 cards do not have the S/PDIF CD-ROM mode */
  263. if (chip->device_id == DEVICE_ID_56301)
  264. control_reg |= GML_SPDIF_CDROM_MODE;
  265. break;
  266. case DIGITAL_MODE_SPDIF_RCA:
  267. /* GML_SPDIF_OPTICAL_MODE bit cleared */
  268. break;
  269. case DIGITAL_MODE_ADAT:
  270. control_reg |= GML_ADAT_MODE;
  271. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  272. break;
  273. }
  274. err = write_control_reg(chip, control_reg, TRUE);
  275. spin_unlock_irq(&chip->lock);
  276. if (err < 0)
  277. return err;
  278. chip->digital_mode = mode;
  279. DE_ACT(("set_digital_mode to %d\n", chip->digital_mode));
  280. return incompatible_clock;
  281. }