layla24_dsp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16. MA 02111-1307, USA.
  17. *************************************************************************
  18. Translation from C++ and adaptation for use in ALSA-Driver
  19. were made by Giuliano Pochini <pochini@shiny.it>
  20. ****************************************************************************/
  21. static int write_control_reg(struct echoaudio *chip, u32 value, char force);
  22. static int set_input_clock(struct echoaudio *chip, u16 clock);
  23. static int set_professional_spdif(struct echoaudio *chip, char prof);
  24. static int set_digital_mode(struct echoaudio *chip, u8 mode);
  25. static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic);
  26. static int check_asic_status(struct echoaudio *chip);
  27. static int init_hw(struct echoaudio *chip, u16 device_id, u16 subdevice_id)
  28. {
  29. int err;
  30. DE_INIT(("init_hw() - Layla24\n"));
  31. if (snd_BUG_ON((subdevice_id & 0xfff0) != LAYLA24))
  32. return -ENODEV;
  33. if ((err = init_dsp_comm_page(chip))) {
  34. DE_INIT(("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_LAYLA24_DSP;
  42. chip->input_clock_types =
  43. ECHO_CLOCK_BIT_INTERNAL | ECHO_CLOCK_BIT_SPDIF |
  44. ECHO_CLOCK_BIT_WORD | ECHO_CLOCK_BIT_ADAT;
  45. chip->digital_modes =
  46. ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_RCA |
  47. ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_OPTICAL |
  48. ECHOCAPS_HAS_DIGITAL_MODE_ADAT;
  49. if ((err = load_firmware(chip)) < 0)
  50. return err;
  51. chip->bad_board = FALSE;
  52. if ((err = init_line_levels(chip)) < 0)
  53. return err;
  54. DE_INIT(("init_hw done\n"));
  55. return err;
  56. }
  57. static int set_mixer_defaults(struct echoaudio *chip)
  58. {
  59. chip->digital_mode = DIGITAL_MODE_SPDIF_RCA;
  60. chip->professional_spdif = FALSE;
  61. chip->digital_in_automute = TRUE;
  62. return init_line_levels(chip);
  63. }
  64. static u32 detect_input_clocks(const struct echoaudio *chip)
  65. {
  66. u32 clocks_from_dsp, clock_bits;
  67. /* Map the DSP clock detect bits to the generic driver clock detect bits */
  68. clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
  69. clock_bits = ECHO_CLOCK_BIT_INTERNAL;
  70. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_SPDIF)
  71. clock_bits |= ECHO_CLOCK_BIT_SPDIF;
  72. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_ADAT)
  73. clock_bits |= ECHO_CLOCK_BIT_ADAT;
  74. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_WORD)
  75. clock_bits |= ECHO_CLOCK_BIT_WORD;
  76. return clock_bits;
  77. }
  78. /* Layla24 has an ASIC on the PCI card and another ASIC in the external box;
  79. both need to be loaded. */
  80. static int load_asic(struct echoaudio *chip)
  81. {
  82. int err;
  83. if (chip->asic_loaded)
  84. return 1;
  85. DE_INIT(("load_asic\n"));
  86. /* Give the DSP a few milliseconds to settle down */
  87. mdelay(10);
  88. /* Load the ASIC for the PCI card */
  89. err = load_asic_generic(chip, DSP_FNC_LOAD_LAYLA24_PCI_CARD_ASIC,
  90. FW_LAYLA24_1_ASIC);
  91. if (err < 0)
  92. return err;
  93. chip->asic_code = FW_LAYLA24_2S_ASIC;
  94. /* Now give the new ASIC a little time to set up */
  95. mdelay(10);
  96. /* Do the external one */
  97. err = load_asic_generic(chip, DSP_FNC_LOAD_LAYLA24_EXTERNAL_ASIC,
  98. FW_LAYLA24_2S_ASIC);
  99. if (err < 0)
  100. return FALSE;
  101. /* Now give the external ASIC a little time to set up */
  102. mdelay(10);
  103. /* See if it worked */
  104. err = check_asic_status(chip);
  105. /* Set up the control register if the load succeeded -
  106. 48 kHz, internal clock, S/PDIF RCA mode */
  107. if (!err)
  108. err = write_control_reg(chip, GML_CONVERTER_ENABLE | GML_48KHZ,
  109. TRUE);
  110. DE_INIT(("load_asic() done\n"));
  111. return err;
  112. }
  113. static int set_sample_rate(struct echoaudio *chip, u32 rate)
  114. {
  115. u32 control_reg, clock, base_rate;
  116. if (snd_BUG_ON(rate >= 50000 &&
  117. chip->digital_mode == DIGITAL_MODE_ADAT))
  118. return -EINVAL;
  119. /* Only set the clock for internal mode. */
  120. if (chip->input_clock != ECHO_CLOCK_INTERNAL) {
  121. DE_ACT(("set_sample_rate: Cannot set sample rate - "
  122. "clock not set to CLK_CLOCKININTERNAL\n"));
  123. /* Save the rate anyhow */
  124. chip->comm_page->sample_rate = cpu_to_le32(rate);
  125. chip->sample_rate = rate;
  126. return 0;
  127. }
  128. /* Get the control register & clear the appropriate bits */
  129. control_reg = le32_to_cpu(chip->comm_page->control_register);
  130. control_reg &= GML_CLOCK_CLEAR_MASK & GML_SPDIF_RATE_CLEAR_MASK;
  131. clock = 0;
  132. switch (rate) {
  133. case 96000:
  134. clock = GML_96KHZ;
  135. break;
  136. case 88200:
  137. clock = GML_88KHZ;
  138. break;
  139. case 48000:
  140. clock = GML_48KHZ | GML_SPDIF_SAMPLE_RATE1;
  141. break;
  142. case 44100:
  143. clock = GML_44KHZ;
  144. /* Professional mode */
  145. if (control_reg & GML_SPDIF_PRO_MODE)
  146. clock |= GML_SPDIF_SAMPLE_RATE0;
  147. break;
  148. case 32000:
  149. clock = GML_32KHZ | GML_SPDIF_SAMPLE_RATE0 |
  150. GML_SPDIF_SAMPLE_RATE1;
  151. break;
  152. case 22050:
  153. clock = GML_22KHZ;
  154. break;
  155. case 16000:
  156. clock = GML_16KHZ;
  157. break;
  158. case 11025:
  159. clock = GML_11KHZ;
  160. break;
  161. case 8000:
  162. clock = GML_8KHZ;
  163. break;
  164. default:
  165. /* If this is a non-standard rate, then the driver needs to
  166. use Layla24's special "continuous frequency" mode */
  167. clock = LAYLA24_CONTINUOUS_CLOCK;
  168. if (rate > 50000) {
  169. base_rate = rate >> 1;
  170. control_reg |= GML_DOUBLE_SPEED_MODE;
  171. } else {
  172. base_rate = rate;
  173. }
  174. if (base_rate < 25000)
  175. base_rate = 25000;
  176. if (wait_handshake(chip))
  177. return -EIO;
  178. chip->comm_page->sample_rate =
  179. cpu_to_le32(LAYLA24_MAGIC_NUMBER / base_rate - 2);
  180. clear_handshake(chip);
  181. send_vector(chip, DSP_VC_SET_LAYLA24_FREQUENCY_REG);
  182. }
  183. control_reg |= clock;
  184. chip->comm_page->sample_rate = cpu_to_le32(rate); /* ignored by the DSP ? */
  185. chip->sample_rate = rate;
  186. DE_ACT(("set_sample_rate: %d clock %d\n", rate, control_reg));
  187. return write_control_reg(chip, control_reg, FALSE);
  188. }
  189. static int set_input_clock(struct echoaudio *chip, u16 clock)
  190. {
  191. u32 control_reg, clocks_from_dsp;
  192. /* Mask off the clock select bits */
  193. control_reg = le32_to_cpu(chip->comm_page->control_register) &
  194. GML_CLOCK_CLEAR_MASK;
  195. clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
  196. /* Pick the new clock */
  197. switch (clock) {
  198. case ECHO_CLOCK_INTERNAL:
  199. DE_ACT(("Set Layla24 clock to INTERNAL\n"));
  200. chip->input_clock = ECHO_CLOCK_INTERNAL;
  201. return set_sample_rate(chip, chip->sample_rate);
  202. case ECHO_CLOCK_SPDIF:
  203. if (chip->digital_mode == DIGITAL_MODE_ADAT)
  204. return -EAGAIN;
  205. control_reg |= GML_SPDIF_CLOCK;
  206. /* Layla24 doesn't support 96KHz S/PDIF */
  207. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  208. DE_ACT(("Set Layla24 clock to SPDIF\n"));
  209. break;
  210. case ECHO_CLOCK_WORD:
  211. control_reg |= GML_WORD_CLOCK;
  212. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_WORD96)
  213. control_reg |= GML_DOUBLE_SPEED_MODE;
  214. else
  215. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  216. DE_ACT(("Set Layla24 clock to WORD\n"));
  217. break;
  218. case ECHO_CLOCK_ADAT:
  219. if (chip->digital_mode != DIGITAL_MODE_ADAT)
  220. return -EAGAIN;
  221. control_reg |= GML_ADAT_CLOCK;
  222. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  223. DE_ACT(("Set Layla24 clock to ADAT\n"));
  224. break;
  225. default:
  226. DE_ACT(("Input clock 0x%x not supported for Layla24\n", clock));
  227. return -EINVAL;
  228. }
  229. chip->input_clock = clock;
  230. return write_control_reg(chip, control_reg, TRUE);
  231. }
  232. /* Depending on what digital mode you want, Layla24 needs different ASICs
  233. loaded. This function checks the ASIC needed for the new mode and sees
  234. if it matches the one already loaded. */
  235. static int switch_asic(struct echoaudio *chip, short asic)
  236. {
  237. s8 *monitors;
  238. /* Check to see if this is already loaded */
  239. if (asic != chip->asic_code) {
  240. monitors = kmemdup(chip->comm_page->monitors,
  241. MONITOR_ARRAY_SIZE, GFP_KERNEL);
  242. if (! monitors)
  243. return -ENOMEM;
  244. memset(chip->comm_page->monitors, ECHOGAIN_MUTED,
  245. MONITOR_ARRAY_SIZE);
  246. /* Load the desired ASIC */
  247. if (load_asic_generic(chip, DSP_FNC_LOAD_LAYLA24_EXTERNAL_ASIC,
  248. asic) < 0) {
  249. memcpy(chip->comm_page->monitors, monitors,
  250. MONITOR_ARRAY_SIZE);
  251. kfree(monitors);
  252. return -EIO;
  253. }
  254. chip->asic_code = asic;
  255. memcpy(chip->comm_page->monitors, monitors, MONITOR_ARRAY_SIZE);
  256. kfree(monitors);
  257. }
  258. return 0;
  259. }
  260. static int dsp_set_digital_mode(struct echoaudio *chip, u8 mode)
  261. {
  262. u32 control_reg;
  263. int err, incompatible_clock;
  264. short asic;
  265. /* Set clock to "internal" if it's not compatible with the new mode */
  266. incompatible_clock = FALSE;
  267. switch (mode) {
  268. case DIGITAL_MODE_SPDIF_OPTICAL:
  269. case DIGITAL_MODE_SPDIF_RCA:
  270. if (chip->input_clock == ECHO_CLOCK_ADAT)
  271. incompatible_clock = TRUE;
  272. asic = FW_LAYLA24_2S_ASIC;
  273. break;
  274. case DIGITAL_MODE_ADAT:
  275. if (chip->input_clock == ECHO_CLOCK_SPDIF)
  276. incompatible_clock = TRUE;
  277. asic = FW_LAYLA24_2A_ASIC;
  278. break;
  279. default:
  280. DE_ACT(("Digital mode not supported: %d\n", mode));
  281. return -EINVAL;
  282. }
  283. if (incompatible_clock) { /* Switch to 48KHz, internal */
  284. chip->sample_rate = 48000;
  285. spin_lock_irq(&chip->lock);
  286. set_input_clock(chip, ECHO_CLOCK_INTERNAL);
  287. spin_unlock_irq(&chip->lock);
  288. }
  289. /* switch_asic() can sleep */
  290. if (switch_asic(chip, asic) < 0)
  291. return -EIO;
  292. spin_lock_irq(&chip->lock);
  293. /* Tweak the control register */
  294. control_reg = le32_to_cpu(chip->comm_page->control_register);
  295. control_reg &= GML_DIGITAL_MODE_CLEAR_MASK;
  296. switch (mode) {
  297. case DIGITAL_MODE_SPDIF_OPTICAL:
  298. control_reg |= GML_SPDIF_OPTICAL_MODE;
  299. break;
  300. case DIGITAL_MODE_SPDIF_RCA:
  301. /* GML_SPDIF_OPTICAL_MODE bit cleared */
  302. break;
  303. case DIGITAL_MODE_ADAT:
  304. control_reg |= GML_ADAT_MODE;
  305. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  306. break;
  307. }
  308. err = write_control_reg(chip, control_reg, TRUE);
  309. spin_unlock_irq(&chip->lock);
  310. if (err < 0)
  311. return err;
  312. chip->digital_mode = mode;
  313. DE_ACT(("set_digital_mode to %d\n", mode));
  314. return incompatible_clock;
  315. }