Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * C-Media CMI8788 driver - main driver module |
| 3 | * |
| 4 | * Copyright (c) Clemens Ladisch <clemens@ladisch.de> |
| 5 | * |
| 6 | * |
| 7 | * This driver is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License, version 2. |
| 9 | * |
| 10 | * This driver 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 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this driver; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 20 | #include <linux/delay.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <sound/ac97_codec.h> |
| 25 | #include <sound/asoundef.h> |
| 26 | #include <sound/core.h> |
| 27 | #include <sound/info.h> |
| 28 | #include <sound/mpu401.h> |
| 29 | #include <sound/pcm.h> |
| 30 | #include "oxygen.h" |
Clemens Ladisch | 878ac3e | 2008-01-21 08:50:19 +0100 | [diff] [blame] | 31 | #include "cm9780.h" |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 32 | |
| 33 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); |
| 34 | MODULE_DESCRIPTION("C-Media CMI8788 helper library"); |
| 35 | MODULE_LICENSE("GPL"); |
| 36 | |
| 37 | |
| 38 | static irqreturn_t oxygen_interrupt(int dummy, void *dev_id) |
| 39 | { |
| 40 | struct oxygen *chip = dev_id; |
| 41 | unsigned int status, clear, elapsed_streams, i; |
| 42 | |
| 43 | status = oxygen_read16(chip, OXYGEN_INTERRUPT_STATUS); |
| 44 | if (!status) |
| 45 | return IRQ_NONE; |
| 46 | |
| 47 | spin_lock(&chip->reg_lock); |
| 48 | |
| 49 | clear = status & (OXYGEN_CHANNEL_A | |
| 50 | OXYGEN_CHANNEL_B | |
| 51 | OXYGEN_CHANNEL_C | |
| 52 | OXYGEN_CHANNEL_SPDIF | |
| 53 | OXYGEN_CHANNEL_MULTICH | |
| 54 | OXYGEN_CHANNEL_AC97 | |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 55 | OXYGEN_INT_SPDIF_IN_DETECT | |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 56 | OXYGEN_INT_GPIO); |
| 57 | if (clear) { |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 58 | if (clear & OXYGEN_INT_SPDIF_IN_DETECT) |
| 59 | chip->interrupt_mask &= ~OXYGEN_INT_SPDIF_IN_DETECT; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 60 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, |
| 61 | chip->interrupt_mask & ~clear); |
| 62 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, |
| 63 | chip->interrupt_mask); |
| 64 | } |
| 65 | |
| 66 | elapsed_streams = status & chip->pcm_running; |
| 67 | |
| 68 | spin_unlock(&chip->reg_lock); |
| 69 | |
| 70 | for (i = 0; i < PCM_COUNT; ++i) |
| 71 | if ((elapsed_streams & (1 << i)) && chip->streams[i]) |
| 72 | snd_pcm_period_elapsed(chip->streams[i]); |
| 73 | |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 74 | if (status & OXYGEN_INT_SPDIF_IN_DETECT) { |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 75 | spin_lock(&chip->reg_lock); |
| 76 | i = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL); |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame^] | 77 | if (i & (OXYGEN_SPDIF_SENSE_INT | OXYGEN_SPDIF_LOCK_INT | |
| 78 | OXYGEN_SPDIF_RATE_INT)) { |
| 79 | /* write the interrupt bit(s) to clear */ |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 80 | oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, i); |
| 81 | schedule_work(&chip->spdif_input_bits_work); |
| 82 | } |
| 83 | spin_unlock(&chip->reg_lock); |
| 84 | } |
| 85 | |
| 86 | if (status & OXYGEN_INT_GPIO) |
| 87 | ; |
| 88 | |
| 89 | if ((status & OXYGEN_INT_MIDI) && chip->midi) |
| 90 | snd_mpu401_uart_interrupt(0, chip->midi->private_data); |
| 91 | |
| 92 | return IRQ_HANDLED; |
| 93 | } |
| 94 | |
| 95 | static void oxygen_spdif_input_bits_changed(struct work_struct *work) |
| 96 | { |
| 97 | struct oxygen *chip = container_of(work, struct oxygen, |
| 98 | spdif_input_bits_work); |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame^] | 99 | u32 reg; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 100 | |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame^] | 101 | /* |
| 102 | * This function gets called when there is new activity on the SPDIF |
| 103 | * input, or when we lose lock on the input signal, or when the rate |
| 104 | * changes. |
| 105 | */ |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 106 | msleep(1); |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame^] | 107 | spin_lock_irq(&chip->reg_lock); |
| 108 | reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL); |
| 109 | if ((reg & (OXYGEN_SPDIF_SENSE_STATUS | |
| 110 | OXYGEN_SPDIF_LOCK_STATUS)) |
| 111 | == OXYGEN_SPDIF_SENSE_STATUS) { |
| 112 | /* |
| 113 | * If we detect activity on the SPDIF input but cannot lock to |
| 114 | * a signal, the clock bit is likely to be wrong. |
| 115 | */ |
| 116 | reg ^= OXYGEN_SPDIF_IN_CLOCK_MASK; |
| 117 | oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 118 | spin_unlock_irq(&chip->reg_lock); |
| 119 | msleep(1); |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame^] | 120 | spin_lock_irq(&chip->reg_lock); |
| 121 | reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL); |
| 122 | if ((reg & (OXYGEN_SPDIF_SENSE_STATUS | |
| 123 | OXYGEN_SPDIF_LOCK_STATUS)) |
| 124 | == OXYGEN_SPDIF_SENSE_STATUS) { |
| 125 | /* nothing detected with either clock; give up */ |
| 126 | if ((reg & OXYGEN_SPDIF_IN_CLOCK_MASK) |
| 127 | == OXYGEN_SPDIF_IN_CLOCK_192) { |
| 128 | /* |
| 129 | * Reset clock to <= 96 kHz because this is |
| 130 | * more likely to be received next time. |
| 131 | */ |
| 132 | reg &= ~OXYGEN_SPDIF_IN_CLOCK_MASK; |
| 133 | reg |= OXYGEN_SPDIF_IN_CLOCK_96; |
| 134 | oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg); |
| 135 | } |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 136 | } |
| 137 | } |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame^] | 138 | spin_unlock_irq(&chip->reg_lock); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 139 | |
Clemens Ladisch | 01a3aff | 2008-01-14 08:56:01 +0100 | [diff] [blame] | 140 | if (chip->controls[CONTROL_SPDIF_INPUT_BITS]) { |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 141 | spin_lock_irq(&chip->reg_lock); |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 142 | chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 143 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, |
| 144 | chip->interrupt_mask); |
| 145 | spin_unlock_irq(&chip->reg_lock); |
| 146 | |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame^] | 147 | /* |
| 148 | * We don't actually know that any channel status bits have |
| 149 | * changed, but let's send a notification just to be sure. |
| 150 | */ |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 151 | snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, |
Clemens Ladisch | 01a3aff | 2008-01-14 08:56:01 +0100 | [diff] [blame] | 152 | &chip->controls[CONTROL_SPDIF_INPUT_BITS]->id); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | #ifdef CONFIG_PROC_FS |
| 157 | static void oxygen_proc_read(struct snd_info_entry *entry, |
| 158 | struct snd_info_buffer *buffer) |
| 159 | { |
| 160 | struct oxygen *chip = entry->private_data; |
| 161 | int i, j; |
| 162 | |
| 163 | snd_iprintf(buffer, "CMI8788\n\n"); |
| 164 | for (i = 0; i < 0x100; i += 0x10) { |
| 165 | snd_iprintf(buffer, "%02x:", i); |
| 166 | for (j = 0; j < 0x10; ++j) |
| 167 | snd_iprintf(buffer, " %02x", oxygen_read8(chip, i + j)); |
| 168 | snd_iprintf(buffer, "\n"); |
| 169 | } |
| 170 | if (mutex_lock_interruptible(&chip->mutex) < 0) |
| 171 | return; |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 172 | if (chip->has_ac97_0) { |
| 173 | snd_iprintf(buffer, "\nAC97\n"); |
| 174 | for (i = 0; i < 0x80; i += 0x10) { |
| 175 | snd_iprintf(buffer, "%02x:", i); |
| 176 | for (j = 0; j < 0x10; j += 2) |
| 177 | snd_iprintf(buffer, " %04x", |
| 178 | oxygen_read_ac97(chip, 0, i + j)); |
| 179 | snd_iprintf(buffer, "\n"); |
| 180 | } |
| 181 | } |
| 182 | if (chip->has_ac97_1) { |
| 183 | snd_iprintf(buffer, "\nAC97 2\n"); |
| 184 | for (i = 0; i < 0x80; i += 0x10) { |
| 185 | snd_iprintf(buffer, "%02x:", i); |
| 186 | for (j = 0; j < 0x10; j += 2) |
| 187 | snd_iprintf(buffer, " %04x", |
| 188 | oxygen_read_ac97(chip, 1, i + j)); |
| 189 | snd_iprintf(buffer, "\n"); |
| 190 | } |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 191 | } |
| 192 | mutex_unlock(&chip->mutex); |
| 193 | } |
| 194 | |
| 195 | static void __devinit oxygen_proc_init(struct oxygen *chip) |
| 196 | { |
| 197 | struct snd_info_entry *entry; |
| 198 | |
| 199 | if (!snd_card_proc_new(chip->card, "cmi8788", &entry)) |
| 200 | snd_info_set_text_ops(entry, chip, oxygen_proc_read); |
| 201 | } |
| 202 | #else |
| 203 | #define oxygen_proc_init(chip) |
| 204 | #endif |
| 205 | |
| 206 | static void __devinit oxygen_init(struct oxygen *chip) |
| 207 | { |
| 208 | unsigned int i; |
| 209 | |
| 210 | chip->dac_routing = 1; |
| 211 | for (i = 0; i < 8; ++i) |
| 212 | chip->dac_volume[i] = 0xff; |
| 213 | chip->spdif_playback_enable = 1; |
| 214 | chip->spdif_bits = OXYGEN_SPDIF_C | OXYGEN_SPDIF_ORIGINAL | |
| 215 | (IEC958_AES1_CON_PCM_CODER << OXYGEN_SPDIF_CATEGORY_SHIFT); |
| 216 | chip->spdif_pcm_bits = chip->spdif_bits; |
| 217 | |
| 218 | if (oxygen_read8(chip, OXYGEN_REVISION) & OXYGEN_REVISION_2) |
| 219 | chip->revision = 2; |
| 220 | else |
| 221 | chip->revision = 1; |
| 222 | |
| 223 | if (chip->revision == 1) |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 224 | oxygen_set_bits8(chip, OXYGEN_MISC, |
| 225 | OXYGEN_MISC_PCI_MEM_W_1_CLOCK); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 226 | |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 227 | i = oxygen_read16(chip, OXYGEN_AC97_CONTROL); |
| 228 | chip->has_ac97_0 = (i & OXYGEN_AC97_CODEC_0) != 0; |
| 229 | chip->has_ac97_1 = (i & OXYGEN_AC97_CODEC_1) != 0; |
| 230 | |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 231 | oxygen_set_bits8(chip, OXYGEN_FUNCTION, |
| 232 | OXYGEN_FUNCTION_RESET_CODEC | |
Clemens Ladisch | 84aa6b7 | 2008-01-16 08:28:54 +0100 | [diff] [blame] | 233 | chip->model->function_flags); |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 234 | oxygen_write16(chip, OXYGEN_I2S_MULTICH_FORMAT, |
| 235 | OXYGEN_RATE_48000 | OXYGEN_I2S_FORMAT_LJUST | |
| 236 | OXYGEN_I2S_MCLK_128 | OXYGEN_I2S_BITS_16 | |
| 237 | OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64); |
| 238 | oxygen_write16(chip, OXYGEN_I2S_A_FORMAT, |
| 239 | OXYGEN_RATE_48000 | OXYGEN_I2S_FORMAT_LJUST | |
| 240 | OXYGEN_I2S_MCLK_128 | OXYGEN_I2S_BITS_16 | |
| 241 | OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64); |
| 242 | oxygen_write16(chip, OXYGEN_I2S_B_FORMAT, |
| 243 | OXYGEN_RATE_48000 | OXYGEN_I2S_FORMAT_LJUST | |
| 244 | OXYGEN_I2S_MCLK_128 | OXYGEN_I2S_BITS_16 | |
| 245 | OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64); |
| 246 | oxygen_write16(chip, OXYGEN_I2S_C_FORMAT, |
| 247 | OXYGEN_RATE_48000 | OXYGEN_I2S_FORMAT_LJUST | |
| 248 | OXYGEN_I2S_MCLK_128 | OXYGEN_I2S_BITS_16 | |
| 249 | OXYGEN_I2S_MASTER | OXYGEN_I2S_BCLK_64); |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame^] | 250 | oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL, |
| 251 | OXYGEN_SPDIF_SENSE_MASK | |
| 252 | OXYGEN_SPDIF_LOCK_MASK | |
| 253 | OXYGEN_SPDIF_RATE_MASK | |
| 254 | OXYGEN_SPDIF_LOCK_PAR | |
| 255 | OXYGEN_SPDIF_IN_CLOCK_96, |
| 256 | OXYGEN_SPDIF_OUT_ENABLE | |
| 257 | OXYGEN_SPDIF_LOOPBACK | |
| 258 | OXYGEN_SPDIF_SENSE_MASK | |
| 259 | OXYGEN_SPDIF_LOCK_MASK | |
| 260 | OXYGEN_SPDIF_RATE_MASK | |
| 261 | OXYGEN_SPDIF_SENSE_PAR | |
| 262 | OXYGEN_SPDIF_LOCK_PAR | |
| 263 | OXYGEN_SPDIF_IN_CLOCK_MASK); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 264 | oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS, chip->spdif_bits); |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 265 | oxygen_write16(chip, OXYGEN_PLAY_ROUTING, |
| 266 | OXYGEN_PLAY_MULTICH_I2S_DAC | OXYGEN_PLAY_SPDIF_SPDIF | |
| 267 | (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | |
| 268 | (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | |
| 269 | (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | |
| 270 | (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT)); |
| 271 | oxygen_write8(chip, OXYGEN_REC_ROUTING, |
| 272 | OXYGEN_REC_A_ROUTE_I2S_ADC_1 | |
| 273 | OXYGEN_REC_B_ROUTE_AC97_1 | |
| 274 | OXYGEN_REC_C_ROUTE_SPDIF); |
| 275 | oxygen_write8(chip, OXYGEN_ADC_MONITOR, 0); |
| 276 | oxygen_write8(chip, OXYGEN_A_MONITOR_ROUTING, |
| 277 | (0 << OXYGEN_A_MONITOR_ROUTE_0_SHIFT) | |
| 278 | (1 << OXYGEN_A_MONITOR_ROUTE_1_SHIFT) | |
| 279 | (2 << OXYGEN_A_MONITOR_ROUTE_2_SHIFT) | |
| 280 | (3 << OXYGEN_A_MONITOR_ROUTE_3_SHIFT)); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 281 | |
| 282 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0); |
| 283 | oxygen_write16(chip, OXYGEN_DMA_STATUS, 0); |
| 284 | |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 285 | oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK, 0); |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 286 | if (chip->has_ac97_0) { |
| 287 | oxygen_clear_bits16(chip, OXYGEN_AC97_OUT_CONFIG, |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 288 | OXYGEN_AC97_CODEC0_FRONTL | |
| 289 | OXYGEN_AC97_CODEC0_FRONTR | |
| 290 | OXYGEN_AC97_CODEC0_SIDEL | |
| 291 | OXYGEN_AC97_CODEC0_SIDER | |
| 292 | OXYGEN_AC97_CODEC0_CENTER | |
| 293 | OXYGEN_AC97_CODEC0_BASE | |
| 294 | OXYGEN_AC97_CODEC0_REARL | |
| 295 | OXYGEN_AC97_CODEC0_REARR); |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 296 | oxygen_set_bits16(chip, OXYGEN_AC97_IN_CONFIG, |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 297 | OXYGEN_AC97_CODEC0_LINEL | |
| 298 | OXYGEN_AC97_CODEC0_LINER); |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 299 | oxygen_write_ac97(chip, 0, AC97_RESET, 0); |
| 300 | msleep(1); |
Clemens Ladisch | 878ac3e | 2008-01-21 08:50:19 +0100 | [diff] [blame] | 301 | oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_SETUP, |
| 302 | CM9780_GPIO0IO | CM9780_GPIO1IO); |
| 303 | oxygen_ac97_set_bits(chip, 0, CM9780_MIXER, |
| 304 | CM9780_BSTSEL | CM9780_STRO_MIC | |
| 305 | CM9780_MIX2FR | CM9780_PCBSW); |
| 306 | oxygen_ac97_set_bits(chip, 0, CM9780_JACK, |
| 307 | CM9780_RSOE | CM9780_CBOE | |
| 308 | CM9780_SSOE | CM9780_FROE | |
| 309 | CM9780_MIC2MIC | CM9780_LI2LI); |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 310 | oxygen_write_ac97(chip, 0, AC97_MASTER, 0x0000); |
| 311 | oxygen_write_ac97(chip, 0, AC97_PC_BEEP, 0x8000); |
| 312 | oxygen_write_ac97(chip, 0, AC97_MIC, 0x8808); |
| 313 | oxygen_write_ac97(chip, 0, AC97_LINE, 0x0808); |
| 314 | oxygen_write_ac97(chip, 0, AC97_CD, 0x8808); |
| 315 | oxygen_write_ac97(chip, 0, AC97_VIDEO, 0x8808); |
| 316 | oxygen_write_ac97(chip, 0, AC97_AUX, 0x8808); |
| 317 | oxygen_write_ac97(chip, 0, AC97_REC_GAIN, 0x8000); |
| 318 | oxygen_write_ac97(chip, 0, AC97_CENTER_LFE_MASTER, 0x8080); |
| 319 | oxygen_write_ac97(chip, 0, AC97_SURROUND_MASTER, 0x8080); |
Clemens Ladisch | 878ac3e | 2008-01-21 08:50:19 +0100 | [diff] [blame] | 320 | oxygen_ac97_clear_bits(chip, 0, |
| 321 | CM9780_GPIO_STATUS, CM9780_GPO0); |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 322 | /* power down unused ADCs and DACs */ |
| 323 | oxygen_ac97_set_bits(chip, 0, AC97_POWERDOWN, |
| 324 | AC97_PD_PR0 | AC97_PD_PR1); |
| 325 | oxygen_ac97_set_bits(chip, 0, AC97_EXTENDED_STATUS, |
| 326 | AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK); |
| 327 | } |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static void oxygen_card_free(struct snd_card *card) |
| 331 | { |
| 332 | struct oxygen *chip = card->private_data; |
| 333 | |
| 334 | spin_lock_irq(&chip->reg_lock); |
| 335 | chip->interrupt_mask = 0; |
| 336 | chip->pcm_running = 0; |
| 337 | oxygen_write16(chip, OXYGEN_DMA_STATUS, 0); |
| 338 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0); |
| 339 | spin_unlock_irq(&chip->reg_lock); |
| 340 | if (chip->irq >= 0) { |
| 341 | free_irq(chip->irq, chip); |
| 342 | synchronize_irq(chip->irq); |
| 343 | } |
| 344 | flush_scheduled_work(); |
| 345 | chip->model->cleanup(chip); |
| 346 | mutex_destroy(&chip->mutex); |
| 347 | pci_release_regions(chip->pci); |
| 348 | pci_disable_device(chip->pci); |
| 349 | } |
| 350 | |
| 351 | int __devinit oxygen_pci_probe(struct pci_dev *pci, int index, char *id, |
Clemens Ladisch | 44fb7aa | 2008-01-21 08:45:37 +0100 | [diff] [blame] | 352 | int midi, const struct oxygen_model *model) |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 353 | { |
| 354 | struct snd_card *card; |
| 355 | struct oxygen *chip; |
| 356 | int err; |
| 357 | |
Clemens Ladisch | 7ef37cd | 2008-01-21 08:51:55 +0100 | [diff] [blame] | 358 | card = snd_card_new(index, id, model->owner, |
| 359 | sizeof *chip + model->model_data_size); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 360 | if (!card) |
| 361 | return -ENOMEM; |
| 362 | |
| 363 | chip = card->private_data; |
| 364 | chip->card = card; |
| 365 | chip->pci = pci; |
| 366 | chip->irq = -1; |
| 367 | chip->model = model; |
Clemens Ladisch | 7ef37cd | 2008-01-21 08:51:55 +0100 | [diff] [blame] | 368 | chip->model_data = chip + 1; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 369 | spin_lock_init(&chip->reg_lock); |
| 370 | mutex_init(&chip->mutex); |
| 371 | INIT_WORK(&chip->spdif_input_bits_work, |
| 372 | oxygen_spdif_input_bits_changed); |
| 373 | |
| 374 | err = pci_enable_device(pci); |
| 375 | if (err < 0) |
| 376 | goto err_card; |
| 377 | |
| 378 | err = pci_request_regions(pci, model->chip); |
| 379 | if (err < 0) { |
| 380 | snd_printk(KERN_ERR "cannot reserve PCI resources\n"); |
| 381 | goto err_pci_enable; |
| 382 | } |
| 383 | |
| 384 | if (!(pci_resource_flags(pci, 0) & IORESOURCE_IO) || |
| 385 | pci_resource_len(pci, 0) < 0x100) { |
| 386 | snd_printk(KERN_ERR "invalid PCI I/O range\n"); |
| 387 | err = -ENXIO; |
| 388 | goto err_pci_regions; |
| 389 | } |
| 390 | chip->addr = pci_resource_start(pci, 0); |
| 391 | |
| 392 | pci_set_master(pci); |
| 393 | snd_card_set_dev(card, &pci->dev); |
| 394 | card->private_free = oxygen_card_free; |
| 395 | |
| 396 | oxygen_init(chip); |
| 397 | model->init(chip); |
| 398 | |
| 399 | err = request_irq(pci->irq, oxygen_interrupt, IRQF_SHARED, |
| 400 | model->chip, chip); |
| 401 | if (err < 0) { |
| 402 | snd_printk(KERN_ERR "cannot grab interrupt %d\n", pci->irq); |
| 403 | goto err_card; |
| 404 | } |
| 405 | chip->irq = pci->irq; |
| 406 | |
| 407 | strcpy(card->driver, model->chip); |
| 408 | strcpy(card->shortname, model->shortname); |
| 409 | sprintf(card->longname, "%s (rev %u) at %#lx, irq %i", |
| 410 | model->longname, chip->revision, chip->addr, chip->irq); |
| 411 | strcpy(card->mixername, model->chip); |
| 412 | snd_component_add(card, model->chip); |
| 413 | |
| 414 | err = oxygen_pcm_init(chip); |
| 415 | if (err < 0) |
| 416 | goto err_card; |
| 417 | |
| 418 | err = oxygen_mixer_init(chip); |
| 419 | if (err < 0) |
| 420 | goto err_card; |
| 421 | |
Clemens Ladisch | 44fb7aa | 2008-01-21 08:45:37 +0100 | [diff] [blame] | 422 | oxygen_write8_masked(chip, OXYGEN_MISC, |
| 423 | midi ? OXYGEN_MISC_MIDI : 0, OXYGEN_MISC_MIDI); |
| 424 | if (midi) { |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 425 | err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI, |
| 426 | chip->addr + OXYGEN_MPU401, |
| 427 | MPU401_INFO_INTEGRATED, 0, 0, |
| 428 | &chip->midi); |
| 429 | if (err < 0) |
| 430 | goto err_card; |
| 431 | } |
| 432 | |
| 433 | oxygen_proc_init(chip); |
| 434 | |
| 435 | spin_lock_irq(&chip->reg_lock); |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 436 | chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 437 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask); |
| 438 | spin_unlock_irq(&chip->reg_lock); |
| 439 | |
| 440 | err = snd_card_register(card); |
| 441 | if (err < 0) |
| 442 | goto err_card; |
| 443 | |
| 444 | pci_set_drvdata(pci, card); |
| 445 | return 0; |
| 446 | |
| 447 | err_pci_regions: |
| 448 | pci_release_regions(pci); |
| 449 | err_pci_enable: |
| 450 | pci_disable_device(pci); |
| 451 | err_card: |
| 452 | snd_card_free(card); |
| 453 | return err; |
| 454 | } |
| 455 | EXPORT_SYMBOL(oxygen_pci_probe); |
| 456 | |
| 457 | void __devexit oxygen_pci_remove(struct pci_dev *pci) |
| 458 | { |
| 459 | snd_card_free(pci_get_drvdata(pci)); |
| 460 | pci_set_drvdata(pci, NULL); |
| 461 | } |
| 462 | EXPORT_SYMBOL(oxygen_pci_remove); |