ecard.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. **********************************************************************
  3. * ecard.c - E-card initialization code
  4. * Copyright 1999, 2000 Creative Labs, Inc.
  5. *
  6. **********************************************************************
  7. *
  8. * Date Author Summary of changes
  9. * ---- ------ ------------------
  10. * October 20, 1999 Bertrand Lee base code release
  11. *
  12. **********************************************************************
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public
  25. * License along with this program; if not, write to the Free
  26. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  27. * USA.
  28. *
  29. **********************************************************************
  30. */
  31. #include "ecard.h"
  32. #include "hwaccess.h"
  33. /* Private routines */
  34. static void ecard_setadcgain(struct emu10k1_card *, struct ecard_state *, u16);
  35. static void ecard_write(struct emu10k1_card *, u32);
  36. /**************************************************************************
  37. * @func Set the gain of the ECARD's CS3310 Trim/gain controller. The
  38. * trim value consists of a 16bit value which is composed of two
  39. * 8 bit gain/trim values, one for the left channel and one for the
  40. * right channel. The following table maps from the Gain/Attenuation
  41. * value in decibels into the corresponding bit pattern for a single
  42. * channel.
  43. */
  44. static void ecard_setadcgain(struct emu10k1_card *card, struct ecard_state *ecard, u16 gain)
  45. {
  46. u32 currbit;
  47. ecard->adc_gain = gain;
  48. /* Enable writing to the TRIM registers */
  49. ecard_write(card, ecard->control_bits & ~EC_TRIM_CSN);
  50. /* Do it again to insure that we meet hold time requirements */
  51. ecard_write(card, ecard->control_bits & ~EC_TRIM_CSN);
  52. for (currbit = (1L << 15); currbit; currbit >>= 1) {
  53. u32 value = ecard->control_bits & ~(EC_TRIM_CSN|EC_TRIM_SDATA);
  54. if (gain & currbit)
  55. value |= EC_TRIM_SDATA;
  56. /* Clock the bit */
  57. ecard_write(card, value);
  58. ecard_write(card, value | EC_TRIM_SCLK);
  59. ecard_write(card, value);
  60. }
  61. ecard_write(card, ecard->control_bits);
  62. }
  63. /**************************************************************************
  64. * @func Clock bits into the Ecard's control latch. The Ecard uses a
  65. * control latch will is loaded bit-serially by toggling the Modem control
  66. * lines from function 2 on the E8010. This function hides these details
  67. * and presents the illusion that we are actually writing to a distinct
  68. * register.
  69. */
  70. static void ecard_write(struct emu10k1_card *card, u32 value)
  71. {
  72. u16 count;
  73. u32 data, hcvalue;
  74. unsigned long flags;
  75. spin_lock_irqsave(&card->lock, flags);
  76. hcvalue = inl(card->iobase + HCFG) & ~(HOOKN_BIT|HANDN_BIT|PULSEN_BIT);
  77. outl(card->iobase + HCFG, hcvalue);
  78. for (count = 0 ; count < EC_NUM_CONTROL_BITS; count++) {
  79. /* Set up the value */
  80. data = ((value & 0x1) ? PULSEN_BIT : 0);
  81. value >>= 1;
  82. outl(card->iobase + HCFG, hcvalue | data);
  83. /* Clock the shift register */
  84. outl(card->iobase + HCFG, hcvalue | data | HANDN_BIT);
  85. outl(card->iobase + HCFG, hcvalue | data);
  86. }
  87. /* Latch the bits */
  88. outl(card->iobase + HCFG, hcvalue | HOOKN_BIT);
  89. outl(card->iobase + HCFG, hcvalue);
  90. spin_unlock_irqrestore(&card->lock, flags);
  91. }
  92. void __devinit emu10k1_ecard_init(struct emu10k1_card *card)
  93. {
  94. u32 hcvalue;
  95. struct ecard_state ecard;
  96. /* Set up the initial settings */
  97. ecard.mux0_setting = EC_DEFAULT_SPDIF0_SEL;
  98. ecard.mux1_setting = EC_DEFAULT_SPDIF1_SEL;
  99. ecard.mux2_setting = 0;
  100. ecard.adc_gain = EC_DEFAULT_ADC_GAIN;
  101. ecard.control_bits = EC_RAW_RUN_MODE |
  102. EC_SPDIF0_SELECT(ecard.mux0_setting) |
  103. EC_SPDIF1_SELECT(ecard.mux1_setting);
  104. /* Step 0: Set the codec type in the hardware control register
  105. * and enable audio output */
  106. hcvalue = emu10k1_readfn0(card, HCFG);
  107. emu10k1_writefn0(card, HCFG, hcvalue | HCFG_AUDIOENABLE | HCFG_CODECFORMAT_I2S);
  108. /* Step 1: Turn off the led and deassert TRIM_CS */
  109. ecard_write(card, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
  110. /* Step 2: Calibrate the ADC and DAC */
  111. ecard_write(card, EC_DACCAL | EC_LEDN | EC_TRIM_CSN);
  112. /* Step 3: Wait for awhile; FIXME: Is this correct? */
  113. current->state = TASK_INTERRUPTIBLE;
  114. schedule_timeout(HZ);
  115. /* Step 4: Switch off the DAC and ADC calibration. Note
  116. * That ADC_CAL is actually an inverted signal, so we assert
  117. * it here to stop calibration. */
  118. ecard_write(card, EC_ADCCAL | EC_LEDN | EC_TRIM_CSN);
  119. /* Step 4: Switch into run mode */
  120. ecard_write(card, ecard.control_bits);
  121. /* Step 5: Set the analog input gain */
  122. ecard_setadcgain(card, &ecard, ecard.adc_gain);
  123. }