mona_dsp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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() - Mona\n"));
  32. if (snd_BUG_ON((subdevice_id & 0xfff0) != MONA))
  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_WORD | ECHO_CLOCK_BIT_ADAT;
  44. chip->digital_modes =
  45. ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_RCA |
  46. ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_OPTICAL |
  47. ECHOCAPS_HAS_DIGITAL_MODE_ADAT;
  48. /* Mona comes in both '301 and '361 flavors */
  49. if (chip->device_id == DEVICE_ID_56361)
  50. chip->dsp_code_to_load = FW_MONA_361_DSP;
  51. else
  52. chip->dsp_code_to_load = FW_MONA_301_DSP;
  53. if ((err = load_firmware(chip)) < 0)
  54. return err;
  55. chip->bad_board = FALSE;
  56. DE_INIT(("init_hw done\n"));
  57. return err;
  58. }
  59. static int set_mixer_defaults(struct echoaudio *chip)
  60. {
  61. chip->digital_mode = DIGITAL_MODE_SPDIF_RCA;
  62. chip->professional_spdif = FALSE;
  63. chip->digital_in_automute = TRUE;
  64. return init_line_levels(chip);
  65. }
  66. static u32 detect_input_clocks(const struct echoaudio *chip)
  67. {
  68. u32 clocks_from_dsp, clock_bits;
  69. /* Map the DSP clock detect bits to the generic driver clock
  70. detect bits */
  71. clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
  72. clock_bits = ECHO_CLOCK_BIT_INTERNAL;
  73. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_SPDIF)
  74. clock_bits |= ECHO_CLOCK_BIT_SPDIF;
  75. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_ADAT)
  76. clock_bits |= ECHO_CLOCK_BIT_ADAT;
  77. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_WORD)
  78. clock_bits |= ECHO_CLOCK_BIT_WORD;
  79. return clock_bits;
  80. }
  81. /* Mona has an ASIC on the PCI card and another ASIC in the external box;
  82. both need to be loaded. */
  83. static int load_asic(struct echoaudio *chip)
  84. {
  85. u32 control_reg;
  86. int err;
  87. short asic;
  88. if (chip->asic_loaded)
  89. return 0;
  90. mdelay(10);
  91. if (chip->device_id == DEVICE_ID_56361)
  92. asic = FW_MONA_361_1_ASIC48;
  93. else
  94. asic = FW_MONA_301_1_ASIC48;
  95. err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC, asic);
  96. if (err < 0)
  97. return err;
  98. chip->asic_code = asic;
  99. mdelay(10);
  100. /* Do the external one */
  101. err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_EXTERNAL_ASIC,
  102. FW_MONA_2_ASIC);
  103. if (err < 0)
  104. return err;
  105. mdelay(10);
  106. err = check_asic_status(chip);
  107. /* Set up the control register if the load succeeded -
  108. 48 kHz, internal clock, S/PDIF RCA mode */
  109. if (!err) {
  110. control_reg = GML_CONVERTER_ENABLE | GML_48KHZ;
  111. err = write_control_reg(chip, control_reg, TRUE);
  112. }
  113. return err;
  114. }
  115. /* Depending on what digital mode you want, Mona needs different ASICs
  116. loaded. This function checks the ASIC needed for the new mode and sees
  117. if it matches the one already loaded. */
  118. static int switch_asic(struct echoaudio *chip, char double_speed)
  119. {
  120. int err;
  121. short asic;
  122. /* Check the clock detect bits to see if this is
  123. a single-speed clock or a double-speed clock; load
  124. a new ASIC if necessary. */
  125. if (chip->device_id == DEVICE_ID_56361) {
  126. if (double_speed)
  127. asic = FW_MONA_361_1_ASIC96;
  128. else
  129. asic = FW_MONA_361_1_ASIC48;
  130. } else {
  131. if (double_speed)
  132. asic = FW_MONA_301_1_ASIC96;
  133. else
  134. asic = FW_MONA_301_1_ASIC48;
  135. }
  136. if (asic != chip->asic_code) {
  137. /* Load the desired ASIC */
  138. err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC,
  139. asic);
  140. if (err < 0)
  141. return err;
  142. chip->asic_code = asic;
  143. }
  144. return 0;
  145. }
  146. static int set_sample_rate(struct echoaudio *chip, u32 rate)
  147. {
  148. u32 control_reg, clock;
  149. short asic;
  150. char force_write;
  151. /* Only set the clock for internal mode. */
  152. if (chip->input_clock != ECHO_CLOCK_INTERNAL) {
  153. DE_ACT(("set_sample_rate: Cannot set sample rate - "
  154. "clock not set to CLK_CLOCKININTERNAL\n"));
  155. /* Save the rate anyhow */
  156. chip->comm_page->sample_rate = cpu_to_le32(rate);
  157. chip->sample_rate = rate;
  158. return 0;
  159. }
  160. /* Now, check to see if the required ASIC is loaded */
  161. if (rate >= 88200) {
  162. if (chip->digital_mode == DIGITAL_MODE_ADAT)
  163. return -EINVAL;
  164. if (chip->device_id == DEVICE_ID_56361)
  165. asic = FW_MONA_361_1_ASIC96;
  166. else
  167. asic = FW_MONA_301_1_ASIC96;
  168. } else {
  169. if (chip->device_id == DEVICE_ID_56361)
  170. asic = FW_MONA_361_1_ASIC48;
  171. else
  172. asic = FW_MONA_301_1_ASIC48;
  173. }
  174. force_write = 0;
  175. if (asic != chip->asic_code) {
  176. int err;
  177. /* Load the desired ASIC (load_asic_generic() can sleep) */
  178. spin_unlock_irq(&chip->lock);
  179. err = load_asic_generic(chip, DSP_FNC_LOAD_MONA_PCI_CARD_ASIC,
  180. asic);
  181. spin_lock_irq(&chip->lock);
  182. if (err < 0)
  183. return err;
  184. chip->asic_code = asic;
  185. force_write = 1;
  186. }
  187. /* Compute the new control register value */
  188. clock = 0;
  189. control_reg = le32_to_cpu(chip->comm_page->control_register);
  190. control_reg &= GML_CLOCK_CLEAR_MASK;
  191. control_reg &= GML_SPDIF_RATE_CLEAR_MASK;
  192. switch (rate) {
  193. case 96000:
  194. clock = GML_96KHZ;
  195. break;
  196. case 88200:
  197. clock = GML_88KHZ;
  198. break;
  199. case 48000:
  200. clock = GML_48KHZ | GML_SPDIF_SAMPLE_RATE1;
  201. break;
  202. case 44100:
  203. clock = GML_44KHZ;
  204. /* Professional mode */
  205. if (control_reg & GML_SPDIF_PRO_MODE)
  206. clock |= GML_SPDIF_SAMPLE_RATE0;
  207. break;
  208. case 32000:
  209. clock = GML_32KHZ | GML_SPDIF_SAMPLE_RATE0 |
  210. GML_SPDIF_SAMPLE_RATE1;
  211. break;
  212. case 22050:
  213. clock = GML_22KHZ;
  214. break;
  215. case 16000:
  216. clock = GML_16KHZ;
  217. break;
  218. case 11025:
  219. clock = GML_11KHZ;
  220. break;
  221. case 8000:
  222. clock = GML_8KHZ;
  223. break;
  224. default:
  225. DE_ACT(("set_sample_rate: %d invalid!\n", rate));
  226. return -EINVAL;
  227. }
  228. control_reg |= clock;
  229. chip->comm_page->sample_rate = cpu_to_le32(rate); /* ignored by the DSP */
  230. chip->sample_rate = rate;
  231. DE_ACT(("set_sample_rate: %d clock %d\n", rate, clock));
  232. return write_control_reg(chip, control_reg, force_write);
  233. }
  234. static int set_input_clock(struct echoaudio *chip, u16 clock)
  235. {
  236. u32 control_reg, clocks_from_dsp;
  237. int err;
  238. DE_ACT(("set_input_clock:\n"));
  239. /* Prevent two simultaneous calls to switch_asic() */
  240. if (atomic_read(&chip->opencount))
  241. return -EAGAIN;
  242. /* Mask off the clock select bits */
  243. control_reg = le32_to_cpu(chip->comm_page->control_register) &
  244. GML_CLOCK_CLEAR_MASK;
  245. clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
  246. switch (clock) {
  247. case ECHO_CLOCK_INTERNAL:
  248. DE_ACT(("Set Mona clock to INTERNAL\n"));
  249. chip->input_clock = ECHO_CLOCK_INTERNAL;
  250. return set_sample_rate(chip, chip->sample_rate);
  251. case ECHO_CLOCK_SPDIF:
  252. if (chip->digital_mode == DIGITAL_MODE_ADAT)
  253. return -EAGAIN;
  254. spin_unlock_irq(&chip->lock);
  255. err = switch_asic(chip, clocks_from_dsp &
  256. GML_CLOCK_DETECT_BIT_SPDIF96);
  257. spin_lock_irq(&chip->lock);
  258. if (err < 0)
  259. return err;
  260. DE_ACT(("Set Mona clock to SPDIF\n"));
  261. control_reg |= GML_SPDIF_CLOCK;
  262. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_SPDIF96)
  263. control_reg |= GML_DOUBLE_SPEED_MODE;
  264. else
  265. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  266. break;
  267. case ECHO_CLOCK_WORD:
  268. DE_ACT(("Set Mona clock to WORD\n"));
  269. spin_unlock_irq(&chip->lock);
  270. err = switch_asic(chip, clocks_from_dsp &
  271. GML_CLOCK_DETECT_BIT_WORD96);
  272. spin_lock_irq(&chip->lock);
  273. if (err < 0)
  274. return err;
  275. control_reg |= GML_WORD_CLOCK;
  276. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_WORD96)
  277. control_reg |= GML_DOUBLE_SPEED_MODE;
  278. else
  279. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  280. break;
  281. case ECHO_CLOCK_ADAT:
  282. DE_ACT(("Set Mona clock to ADAT\n"));
  283. if (chip->digital_mode != DIGITAL_MODE_ADAT)
  284. return -EAGAIN;
  285. control_reg |= GML_ADAT_CLOCK;
  286. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  287. break;
  288. default:
  289. DE_ACT(("Input clock 0x%x not supported for Mona\n", clock));
  290. return -EINVAL;
  291. }
  292. chip->input_clock = clock;
  293. return write_control_reg(chip, control_reg, TRUE);
  294. }
  295. static int dsp_set_digital_mode(struct echoaudio *chip, u8 mode)
  296. {
  297. u32 control_reg;
  298. int err, incompatible_clock;
  299. /* Set clock to "internal" if it's not compatible with the new mode */
  300. incompatible_clock = FALSE;
  301. switch (mode) {
  302. case DIGITAL_MODE_SPDIF_OPTICAL:
  303. case DIGITAL_MODE_SPDIF_RCA:
  304. if (chip->input_clock == ECHO_CLOCK_ADAT)
  305. incompatible_clock = TRUE;
  306. break;
  307. case DIGITAL_MODE_ADAT:
  308. if (chip->input_clock == ECHO_CLOCK_SPDIF)
  309. incompatible_clock = TRUE;
  310. break;
  311. default:
  312. DE_ACT(("Digital mode not supported: %d\n", mode));
  313. return -EINVAL;
  314. }
  315. spin_lock_irq(&chip->lock);
  316. if (incompatible_clock) { /* Switch to 48KHz, internal */
  317. chip->sample_rate = 48000;
  318. set_input_clock(chip, ECHO_CLOCK_INTERNAL);
  319. }
  320. /* Clear the current digital mode */
  321. control_reg = le32_to_cpu(chip->comm_page->control_register);
  322. control_reg &= GML_DIGITAL_MODE_CLEAR_MASK;
  323. /* Tweak the control reg */
  324. switch (mode) {
  325. case DIGITAL_MODE_SPDIF_OPTICAL:
  326. control_reg |= GML_SPDIF_OPTICAL_MODE;
  327. break;
  328. case DIGITAL_MODE_SPDIF_RCA:
  329. /* GML_SPDIF_OPTICAL_MODE bit cleared */
  330. break;
  331. case DIGITAL_MODE_ADAT:
  332. /* If the current ASIC is the 96KHz ASIC, switch the ASIC
  333. and set to 48 KHz */
  334. if (chip->asic_code == FW_MONA_361_1_ASIC96 ||
  335. chip->asic_code == FW_MONA_301_1_ASIC96) {
  336. set_sample_rate(chip, 48000);
  337. }
  338. control_reg |= GML_ADAT_MODE;
  339. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  340. break;
  341. }
  342. err = write_control_reg(chip, control_reg, FALSE);
  343. spin_unlock_irq(&chip->lock);
  344. if (err < 0)
  345. return err;
  346. chip->digital_mode = mode;
  347. DE_ACT(("set_digital_mode to %d\n", mode));
  348. return incompatible_clock;
  349. }