mona_dsp.c 11 KB

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