layla24_dsp.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. if (snd_BUG_ON((subdevice_id & 0xfff0) != LAYLA24))
  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_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. return err;
  55. }
  56. static int set_mixer_defaults(struct echoaudio *chip)
  57. {
  58. chip->digital_mode = DIGITAL_MODE_SPDIF_RCA;
  59. chip->professional_spdif = false;
  60. chip->digital_in_automute = true;
  61. return init_line_levels(chip);
  62. }
  63. static u32 detect_input_clocks(const struct echoaudio *chip)
  64. {
  65. u32 clocks_from_dsp, clock_bits;
  66. /* Map the DSP clock detect bits to the generic driver clock detect bits */
  67. clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
  68. clock_bits = ECHO_CLOCK_BIT_INTERNAL;
  69. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_SPDIF)
  70. clock_bits |= ECHO_CLOCK_BIT_SPDIF;
  71. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_ADAT)
  72. clock_bits |= ECHO_CLOCK_BIT_ADAT;
  73. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_WORD)
  74. clock_bits |= ECHO_CLOCK_BIT_WORD;
  75. return clock_bits;
  76. }
  77. /* Layla24 has an ASIC on the PCI card and another ASIC in the external box;
  78. both need to be loaded. */
  79. static int load_asic(struct echoaudio *chip)
  80. {
  81. int err;
  82. if (chip->asic_loaded)
  83. return 1;
  84. /* Give the DSP a few milliseconds to settle down */
  85. mdelay(10);
  86. /* Load the ASIC for the PCI card */
  87. err = load_asic_generic(chip, DSP_FNC_LOAD_LAYLA24_PCI_CARD_ASIC,
  88. FW_LAYLA24_1_ASIC);
  89. if (err < 0)
  90. return err;
  91. chip->asic_code = FW_LAYLA24_2S_ASIC;
  92. /* Now give the new ASIC a little time to set up */
  93. mdelay(10);
  94. /* Do the external one */
  95. err = load_asic_generic(chip, DSP_FNC_LOAD_LAYLA24_EXTERNAL_ASIC,
  96. FW_LAYLA24_2S_ASIC);
  97. if (err < 0)
  98. return false;
  99. /* Now give the external ASIC a little time to set up */
  100. mdelay(10);
  101. /* See if it worked */
  102. err = check_asic_status(chip);
  103. /* Set up the control register if the load succeeded -
  104. 48 kHz, internal clock, S/PDIF RCA mode */
  105. if (!err)
  106. err = write_control_reg(chip, GML_CONVERTER_ENABLE | GML_48KHZ,
  107. true);
  108. return err;
  109. }
  110. static int set_sample_rate(struct echoaudio *chip, u32 rate)
  111. {
  112. u32 control_reg, clock, base_rate;
  113. if (snd_BUG_ON(rate >= 50000 &&
  114. chip->digital_mode == DIGITAL_MODE_ADAT))
  115. return -EINVAL;
  116. /* Only set the clock for internal mode. */
  117. if (chip->input_clock != ECHO_CLOCK_INTERNAL) {
  118. dev_warn(chip->card->dev,
  119. "Cannot set sample rate - clock not set to CLK_CLOCKININTERNAL\n");
  120. /* Save the rate anyhow */
  121. chip->comm_page->sample_rate = cpu_to_le32(rate);
  122. chip->sample_rate = rate;
  123. return 0;
  124. }
  125. /* Get the control register & clear the appropriate bits */
  126. control_reg = le32_to_cpu(chip->comm_page->control_register);
  127. control_reg &= GML_CLOCK_CLEAR_MASK & GML_SPDIF_RATE_CLEAR_MASK;
  128. clock = 0;
  129. switch (rate) {
  130. case 96000:
  131. clock = GML_96KHZ;
  132. break;
  133. case 88200:
  134. clock = GML_88KHZ;
  135. break;
  136. case 48000:
  137. clock = GML_48KHZ | GML_SPDIF_SAMPLE_RATE1;
  138. break;
  139. case 44100:
  140. clock = GML_44KHZ;
  141. /* Professional mode */
  142. if (control_reg & GML_SPDIF_PRO_MODE)
  143. clock |= GML_SPDIF_SAMPLE_RATE0;
  144. break;
  145. case 32000:
  146. clock = GML_32KHZ | GML_SPDIF_SAMPLE_RATE0 |
  147. GML_SPDIF_SAMPLE_RATE1;
  148. break;
  149. case 22050:
  150. clock = GML_22KHZ;
  151. break;
  152. case 16000:
  153. clock = GML_16KHZ;
  154. break;
  155. case 11025:
  156. clock = GML_11KHZ;
  157. break;
  158. case 8000:
  159. clock = GML_8KHZ;
  160. break;
  161. default:
  162. /* If this is a non-standard rate, then the driver needs to
  163. use Layla24's special "continuous frequency" mode */
  164. clock = LAYLA24_CONTINUOUS_CLOCK;
  165. if (rate > 50000) {
  166. base_rate = rate >> 1;
  167. control_reg |= GML_DOUBLE_SPEED_MODE;
  168. } else {
  169. base_rate = rate;
  170. }
  171. if (base_rate < 25000)
  172. base_rate = 25000;
  173. if (wait_handshake(chip))
  174. return -EIO;
  175. chip->comm_page->sample_rate =
  176. cpu_to_le32(LAYLA24_MAGIC_NUMBER / base_rate - 2);
  177. clear_handshake(chip);
  178. send_vector(chip, DSP_VC_SET_LAYLA24_FREQUENCY_REG);
  179. }
  180. control_reg |= clock;
  181. chip->comm_page->sample_rate = cpu_to_le32(rate); /* ignored by the DSP ? */
  182. chip->sample_rate = rate;
  183. dev_dbg(chip->card->dev,
  184. "set_sample_rate: %d clock %d\n", rate, control_reg);
  185. return write_control_reg(chip, control_reg, false);
  186. }
  187. static int set_input_clock(struct echoaudio *chip, u16 clock)
  188. {
  189. u32 control_reg, clocks_from_dsp;
  190. /* Mask off the clock select bits */
  191. control_reg = le32_to_cpu(chip->comm_page->control_register) &
  192. GML_CLOCK_CLEAR_MASK;
  193. clocks_from_dsp = le32_to_cpu(chip->comm_page->status_clocks);
  194. /* Pick the new clock */
  195. switch (clock) {
  196. case ECHO_CLOCK_INTERNAL:
  197. chip->input_clock = ECHO_CLOCK_INTERNAL;
  198. return set_sample_rate(chip, chip->sample_rate);
  199. case ECHO_CLOCK_SPDIF:
  200. if (chip->digital_mode == DIGITAL_MODE_ADAT)
  201. return -EAGAIN;
  202. control_reg |= GML_SPDIF_CLOCK;
  203. /* Layla24 doesn't support 96KHz S/PDIF */
  204. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  205. break;
  206. case ECHO_CLOCK_WORD:
  207. control_reg |= GML_WORD_CLOCK;
  208. if (clocks_from_dsp & GML_CLOCK_DETECT_BIT_WORD96)
  209. control_reg |= GML_DOUBLE_SPEED_MODE;
  210. else
  211. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  212. break;
  213. case ECHO_CLOCK_ADAT:
  214. if (chip->digital_mode != DIGITAL_MODE_ADAT)
  215. return -EAGAIN;
  216. control_reg |= GML_ADAT_CLOCK;
  217. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  218. break;
  219. default:
  220. dev_err(chip->card->dev,
  221. "Input clock 0x%x not supported for Layla24\n", clock);
  222. return -EINVAL;
  223. }
  224. chip->input_clock = clock;
  225. return write_control_reg(chip, control_reg, true);
  226. }
  227. /* Depending on what digital mode you want, Layla24 needs different ASICs
  228. loaded. This function checks the ASIC needed for the new mode and sees
  229. if it matches the one already loaded. */
  230. static int switch_asic(struct echoaudio *chip, short asic)
  231. {
  232. s8 *monitors;
  233. /* Check to see if this is already loaded */
  234. if (asic != chip->asic_code) {
  235. monitors = kmemdup(chip->comm_page->monitors,
  236. MONITOR_ARRAY_SIZE, GFP_KERNEL);
  237. if (! monitors)
  238. return -ENOMEM;
  239. memset(chip->comm_page->monitors, ECHOGAIN_MUTED,
  240. MONITOR_ARRAY_SIZE);
  241. /* Load the desired ASIC */
  242. if (load_asic_generic(chip, DSP_FNC_LOAD_LAYLA24_EXTERNAL_ASIC,
  243. asic) < 0) {
  244. memcpy(chip->comm_page->monitors, monitors,
  245. MONITOR_ARRAY_SIZE);
  246. kfree(monitors);
  247. return -EIO;
  248. }
  249. chip->asic_code = asic;
  250. memcpy(chip->comm_page->monitors, monitors, MONITOR_ARRAY_SIZE);
  251. kfree(monitors);
  252. }
  253. return 0;
  254. }
  255. static int dsp_set_digital_mode(struct echoaudio *chip, u8 mode)
  256. {
  257. u32 control_reg;
  258. int err, incompatible_clock;
  259. short asic;
  260. /* Set clock to "internal" if it's not compatible with the new mode */
  261. incompatible_clock = false;
  262. switch (mode) {
  263. case DIGITAL_MODE_SPDIF_OPTICAL:
  264. case DIGITAL_MODE_SPDIF_RCA:
  265. if (chip->input_clock == ECHO_CLOCK_ADAT)
  266. incompatible_clock = true;
  267. asic = FW_LAYLA24_2S_ASIC;
  268. break;
  269. case DIGITAL_MODE_ADAT:
  270. if (chip->input_clock == ECHO_CLOCK_SPDIF)
  271. incompatible_clock = true;
  272. asic = FW_LAYLA24_2A_ASIC;
  273. break;
  274. default:
  275. dev_err(chip->card->dev,
  276. "Digital mode not supported: %d\n", mode);
  277. return -EINVAL;
  278. }
  279. if (incompatible_clock) { /* Switch to 48KHz, internal */
  280. chip->sample_rate = 48000;
  281. spin_lock_irq(&chip->lock);
  282. set_input_clock(chip, ECHO_CLOCK_INTERNAL);
  283. spin_unlock_irq(&chip->lock);
  284. }
  285. /* switch_asic() can sleep */
  286. if (switch_asic(chip, asic) < 0)
  287. return -EIO;
  288. spin_lock_irq(&chip->lock);
  289. /* Tweak the control register */
  290. control_reg = le32_to_cpu(chip->comm_page->control_register);
  291. control_reg &= GML_DIGITAL_MODE_CLEAR_MASK;
  292. switch (mode) {
  293. case DIGITAL_MODE_SPDIF_OPTICAL:
  294. control_reg |= GML_SPDIF_OPTICAL_MODE;
  295. break;
  296. case DIGITAL_MODE_SPDIF_RCA:
  297. /* GML_SPDIF_OPTICAL_MODE bit cleared */
  298. break;
  299. case DIGITAL_MODE_ADAT:
  300. control_reg |= GML_ADAT_MODE;
  301. control_reg &= ~GML_DOUBLE_SPEED_MODE;
  302. break;
  303. }
  304. err = write_control_reg(chip, control_reg, true);
  305. spin_unlock_irq(&chip->lock);
  306. if (err < 0)
  307. return err;
  308. chip->digital_mode = mode;
  309. dev_dbg(chip->card->dev, "set_digital_mode to %d\n", mode);
  310. return incompatible_clock;
  311. }