2 * C-Media CMI8788 driver - main driver module
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
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.
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.
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
20 #include <linux/delay.h>
21 #include <linux/interrupt.h>
22 #include <linux/mutex.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <sound/ac97_codec.h>
26 #include <sound/asoundef.h>
27 #include <sound/core.h>
28 #include <sound/info.h>
29 #include <sound/mpu401.h>
30 #include <sound/pcm.h>
34 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
35 MODULE_DESCRIPTION("C-Media CMI8788 helper library");
36 MODULE_LICENSE("GPL v2");
38 #define DRIVER "oxygen"
40 static inline int oxygen_uart_input_ready(struct oxygen
*chip
)
42 return !(oxygen_read8(chip
, OXYGEN_MPU401
+ 1) & MPU401_RX_EMPTY
);
45 static void oxygen_read_uart(struct oxygen
*chip
)
47 if (unlikely(!oxygen_uart_input_ready(chip
))) {
48 /* no data, but read it anyway to clear the interrupt */
49 oxygen_read8(chip
, OXYGEN_MPU401
);
53 u8 data
= oxygen_read8(chip
, OXYGEN_MPU401
);
54 if (data
== MPU401_ACK
)
56 if (chip
->uart_input_count
>= ARRAY_SIZE(chip
->uart_input
))
57 chip
->uart_input_count
= 0;
58 chip
->uart_input
[chip
->uart_input_count
++] = data
;
59 } while (oxygen_uart_input_ready(chip
));
60 if (chip
->model
.uart_input
)
61 chip
->model
.uart_input(chip
);
64 static irqreturn_t
oxygen_interrupt(int dummy
, void *dev_id
)
66 struct oxygen
*chip
= dev_id
;
67 unsigned int status
, clear
, elapsed_streams
, i
;
69 status
= oxygen_read16(chip
, OXYGEN_INTERRUPT_STATUS
);
73 spin_lock(&chip
->reg_lock
);
75 clear
= status
& (OXYGEN_CHANNEL_A
|
78 OXYGEN_CHANNEL_SPDIF
|
79 OXYGEN_CHANNEL_MULTICH
|
81 OXYGEN_INT_SPDIF_IN_DETECT
|
85 if (clear
& OXYGEN_INT_SPDIF_IN_DETECT
)
86 chip
->interrupt_mask
&= ~OXYGEN_INT_SPDIF_IN_DETECT
;
87 oxygen_write16(chip
, OXYGEN_INTERRUPT_MASK
,
88 chip
->interrupt_mask
& ~clear
);
89 oxygen_write16(chip
, OXYGEN_INTERRUPT_MASK
,
90 chip
->interrupt_mask
);
93 elapsed_streams
= status
& chip
->pcm_running
;
95 spin_unlock(&chip
->reg_lock
);
97 for (i
= 0; i
< PCM_COUNT
; ++i
)
98 if ((elapsed_streams
& (1 << i
)) && chip
->streams
[i
])
99 snd_pcm_period_elapsed(chip
->streams
[i
]);
101 if (status
& OXYGEN_INT_SPDIF_IN_DETECT
) {
102 spin_lock(&chip
->reg_lock
);
103 i
= oxygen_read32(chip
, OXYGEN_SPDIF_CONTROL
);
104 if (i
& (OXYGEN_SPDIF_SENSE_INT
| OXYGEN_SPDIF_LOCK_INT
|
105 OXYGEN_SPDIF_RATE_INT
)) {
106 /* write the interrupt bit(s) to clear */
107 oxygen_write32(chip
, OXYGEN_SPDIF_CONTROL
, i
);
108 schedule_work(&chip
->spdif_input_bits_work
);
110 spin_unlock(&chip
->reg_lock
);
113 if (status
& OXYGEN_INT_GPIO
)
114 schedule_work(&chip
->gpio_work
);
116 if (status
& OXYGEN_INT_MIDI
) {
118 snd_mpu401_uart_interrupt(0, chip
->midi
->private_data
);
120 oxygen_read_uart(chip
);
123 if (status
& OXYGEN_INT_AC97
)
124 wake_up(&chip
->ac97_waitqueue
);
129 static void oxygen_spdif_input_bits_changed(struct work_struct
*work
)
131 struct oxygen
*chip
= container_of(work
, struct oxygen
,
132 spdif_input_bits_work
);
136 * This function gets called when there is new activity on the SPDIF
137 * input, or when we lose lock on the input signal, or when the rate
141 spin_lock_irq(&chip
->reg_lock
);
142 reg
= oxygen_read32(chip
, OXYGEN_SPDIF_CONTROL
);
143 if ((reg
& (OXYGEN_SPDIF_SENSE_STATUS
|
144 OXYGEN_SPDIF_LOCK_STATUS
))
145 == OXYGEN_SPDIF_SENSE_STATUS
) {
147 * If we detect activity on the SPDIF input but cannot lock to
148 * a signal, the clock bit is likely to be wrong.
150 reg
^= OXYGEN_SPDIF_IN_CLOCK_MASK
;
151 oxygen_write32(chip
, OXYGEN_SPDIF_CONTROL
, reg
);
152 spin_unlock_irq(&chip
->reg_lock
);
154 spin_lock_irq(&chip
->reg_lock
);
155 reg
= oxygen_read32(chip
, OXYGEN_SPDIF_CONTROL
);
156 if ((reg
& (OXYGEN_SPDIF_SENSE_STATUS
|
157 OXYGEN_SPDIF_LOCK_STATUS
))
158 == OXYGEN_SPDIF_SENSE_STATUS
) {
159 /* nothing detected with either clock; give up */
160 if ((reg
& OXYGEN_SPDIF_IN_CLOCK_MASK
)
161 == OXYGEN_SPDIF_IN_CLOCK_192
) {
163 * Reset clock to <= 96 kHz because this is
164 * more likely to be received next time.
166 reg
&= ~OXYGEN_SPDIF_IN_CLOCK_MASK
;
167 reg
|= OXYGEN_SPDIF_IN_CLOCK_96
;
168 oxygen_write32(chip
, OXYGEN_SPDIF_CONTROL
, reg
);
172 spin_unlock_irq(&chip
->reg_lock
);
174 if (chip
->controls
[CONTROL_SPDIF_INPUT_BITS
]) {
175 spin_lock_irq(&chip
->reg_lock
);
176 chip
->interrupt_mask
|= OXYGEN_INT_SPDIF_IN_DETECT
;
177 oxygen_write16(chip
, OXYGEN_INTERRUPT_MASK
,
178 chip
->interrupt_mask
);
179 spin_unlock_irq(&chip
->reg_lock
);
182 * We don't actually know that any channel status bits have
183 * changed, but let's send a notification just to be sure.
185 snd_ctl_notify(chip
->card
, SNDRV_CTL_EVENT_MASK_VALUE
,
186 &chip
->controls
[CONTROL_SPDIF_INPUT_BITS
]->id
);
190 static void oxygen_gpio_changed(struct work_struct
*work
)
192 struct oxygen
*chip
= container_of(work
, struct oxygen
, gpio_work
);
194 if (chip
->model
.gpio_changed
)
195 chip
->model
.gpio_changed(chip
);
198 #ifdef CONFIG_PROC_FS
199 static void oxygen_proc_read(struct snd_info_entry
*entry
,
200 struct snd_info_buffer
*buffer
)
202 struct oxygen
*chip
= entry
->private_data
;
205 switch (oxygen_read8(chip
, OXYGEN_REVISION
) & OXYGEN_PACKAGE_ID_MASK
) {
206 case OXYGEN_PACKAGE_ID_8786
: i
= '6'; break;
207 case OXYGEN_PACKAGE_ID_8787
: i
= '7'; break;
208 case OXYGEN_PACKAGE_ID_8788
: i
= '8'; break;
209 default: i
= '?'; break;
211 snd_iprintf(buffer
, "CMI878%c:\n", i
);
212 for (i
= 0; i
< OXYGEN_IO_SIZE
; i
+= 0x10) {
213 snd_iprintf(buffer
, "%02x:", i
);
214 for (j
= 0; j
< 0x10; ++j
)
215 snd_iprintf(buffer
, " %02x", oxygen_read8(chip
, i
+ j
));
216 snd_iprintf(buffer
, "\n");
218 if (mutex_lock_interruptible(&chip
->mutex
) < 0)
220 if (chip
->has_ac97_0
) {
221 snd_iprintf(buffer
, "\nAC97:\n");
222 for (i
= 0; i
< 0x80; i
+= 0x10) {
223 snd_iprintf(buffer
, "%02x:", i
);
224 for (j
= 0; j
< 0x10; j
+= 2)
225 snd_iprintf(buffer
, " %04x",
226 oxygen_read_ac97(chip
, 0, i
+ j
));
227 snd_iprintf(buffer
, "\n");
230 if (chip
->has_ac97_1
) {
231 snd_iprintf(buffer
, "\nAC97 2:\n");
232 for (i
= 0; i
< 0x80; i
+= 0x10) {
233 snd_iprintf(buffer
, "%02x:", i
);
234 for (j
= 0; j
< 0x10; j
+= 2)
235 snd_iprintf(buffer
, " %04x",
236 oxygen_read_ac97(chip
, 1, i
+ j
));
237 snd_iprintf(buffer
, "\n");
240 mutex_unlock(&chip
->mutex
);
241 if (chip
->model
.dump_registers
)
242 chip
->model
.dump_registers(chip
, buffer
);
245 static void oxygen_proc_init(struct oxygen
*chip
)
247 struct snd_info_entry
*entry
;
249 if (!snd_card_proc_new(chip
->card
, "oxygen", &entry
))
250 snd_info_set_text_ops(entry
, chip
, oxygen_proc_read
);
253 #define oxygen_proc_init(chip)
256 static const struct pci_device_id
*
257 oxygen_search_pci_id(struct oxygen
*chip
, const struct pci_device_id ids
[])
262 * Make sure the EEPROM pins are available, i.e., not used for SPI.
263 * (This function is called before we initialize or use SPI.)
265 oxygen_clear_bits8(chip
, OXYGEN_FUNCTION
,
266 OXYGEN_FUNCTION_ENABLE_SPI_4_5
);
268 * Read the subsystem device ID directly from the EEPROM, because the
269 * chip didn't if the first EEPROM word was overwritten.
271 subdevice
= oxygen_read_eeprom(chip
, 2);
272 /* use default ID if EEPROM is missing */
273 if (subdevice
== 0xffff && oxygen_read_eeprom(chip
, 1) == 0xffff)
276 * We use only the subsystem device ID for searching because it is
277 * unique even without the subsystem vendor ID, which may have been
278 * overwritten in the EEPROM.
280 for (; ids
->vendor
; ++ids
)
281 if (ids
->subdevice
== subdevice
&&
282 ids
->driver_data
!= BROKEN_EEPROM_DRIVER_DATA
)
287 static void oxygen_restore_eeprom(struct oxygen
*chip
,
288 const struct pci_device_id
*id
)
292 eeprom_id
= oxygen_read_eeprom(chip
, 0);
293 if (eeprom_id
!= OXYGEN_EEPROM_ID
&&
294 (eeprom_id
!= 0xffff || id
->subdevice
!= 0x8788)) {
296 * This function gets called only when a known card model has
297 * been detected, i.e., we know there is a valid subsystem
298 * product ID at index 2 in the EEPROM. Therefore, we have
299 * been able to deduce the correct subsystem vendor ID, and
300 * this is enough information to restore the original EEPROM
303 oxygen_write_eeprom(chip
, 1, id
->subvendor
);
304 oxygen_write_eeprom(chip
, 0, OXYGEN_EEPROM_ID
);
306 oxygen_set_bits8(chip
, OXYGEN_MISC
,
307 OXYGEN_MISC_WRITE_PCI_SUBID
);
308 pci_write_config_word(chip
->pci
, PCI_SUBSYSTEM_VENDOR_ID
,
310 pci_write_config_word(chip
->pci
, PCI_SUBSYSTEM_ID
,
312 oxygen_clear_bits8(chip
, OXYGEN_MISC
,
313 OXYGEN_MISC_WRITE_PCI_SUBID
);
315 snd_printk(KERN_INFO
"EEPROM ID restored\n");
319 static void configure_pcie_bridge(struct pci_dev
*pci
)
321 enum { PEX811X
, PI7C9X110
};
322 static const struct pci_device_id bridge_ids
[] = {
323 { PCI_VDEVICE(PLX
, 0x8111), .driver_data
= PEX811X
},
324 { PCI_VDEVICE(PLX
, 0x8112), .driver_data
= PEX811X
},
325 { PCI_DEVICE(0x12d8, 0xe110), .driver_data
= PI7C9X110
},
328 struct pci_dev
*bridge
;
329 const struct pci_device_id
*id
;
332 if (!pci
->bus
|| !pci
->bus
->self
)
334 bridge
= pci
->bus
->self
;
336 id
= pci_match_id(bridge_ids
, bridge
);
340 switch (id
->driver_data
) {
341 case PEX811X
: /* PLX PEX8111/PEX8112 PCIe/PCI bridge */
342 pci_read_config_dword(bridge
, 0x48, &tmp
);
343 tmp
|= 1; /* enable blind prefetching */
344 tmp
|= 1 << 11; /* enable beacon generation */
345 pci_write_config_dword(bridge
, 0x48, tmp
);
347 pci_write_config_dword(bridge
, 0x84, 0x0c);
348 pci_read_config_dword(bridge
, 0x88, &tmp
);
350 tmp
|= 2 << 27; /* set prefetch size to 128 bytes */
351 pci_write_config_dword(bridge
, 0x88, tmp
);
354 case PI7C9X110
: /* Pericom PI7C9X110 PCIe/PCI bridge */
355 pci_read_config_dword(bridge
, 0x40, &tmp
);
356 tmp
|= 1; /* park the PCI arbiter to the sound chip */
357 pci_write_config_dword(bridge
, 0x40, tmp
);
362 static void oxygen_init(struct oxygen
*chip
)
366 chip
->dac_routing
= 1;
367 for (i
= 0; i
< 8; ++i
)
368 chip
->dac_volume
[i
] = chip
->model
.dac_volume_min
;
370 chip
->spdif_playback_enable
= 1;
371 chip
->spdif_bits
= OXYGEN_SPDIF_C
| OXYGEN_SPDIF_ORIGINAL
|
372 (IEC958_AES1_CON_PCM_CODER
<< OXYGEN_SPDIF_CATEGORY_SHIFT
);
373 chip
->spdif_pcm_bits
= chip
->spdif_bits
;
375 if (oxygen_read8(chip
, OXYGEN_REVISION
) & OXYGEN_REVISION_2
)
380 if (chip
->revision
== 1)
381 oxygen_set_bits8(chip
, OXYGEN_MISC
,
382 OXYGEN_MISC_PCI_MEM_W_1_CLOCK
);
384 i
= oxygen_read16(chip
, OXYGEN_AC97_CONTROL
);
385 chip
->has_ac97_0
= (i
& OXYGEN_AC97_CODEC_0
) != 0;
386 chip
->has_ac97_1
= (i
& OXYGEN_AC97_CODEC_1
) != 0;
388 oxygen_write8_masked(chip
, OXYGEN_FUNCTION
,
389 OXYGEN_FUNCTION_RESET_CODEC
|
390 chip
->model
.function_flags
,
391 OXYGEN_FUNCTION_RESET_CODEC
|
392 OXYGEN_FUNCTION_2WIRE_SPI_MASK
|
393 OXYGEN_FUNCTION_ENABLE_SPI_4_5
);
394 oxygen_write8(chip
, OXYGEN_DMA_STATUS
, 0);
395 oxygen_write8(chip
, OXYGEN_DMA_PAUSE
, 0);
396 oxygen_write8(chip
, OXYGEN_PLAY_CHANNELS
,
397 OXYGEN_PLAY_CHANNELS_2
|
398 OXYGEN_DMA_A_BURST_8
|
399 OXYGEN_DMA_MULTICH_BURST_8
);
400 oxygen_write16(chip
, OXYGEN_INTERRUPT_MASK
, 0);
401 oxygen_write8_masked(chip
, OXYGEN_MISC
,
402 chip
->model
.misc_flags
,
403 OXYGEN_MISC_WRITE_PCI_SUBID
|
404 OXYGEN_MISC_REC_C_FROM_SPDIF
|
405 OXYGEN_MISC_REC_B_FROM_AC97
|
406 OXYGEN_MISC_REC_A_FROM_MULTICH
|
408 oxygen_write8(chip
, OXYGEN_REC_FORMAT
,
409 (OXYGEN_FORMAT_16
<< OXYGEN_REC_FORMAT_A_SHIFT
) |
410 (OXYGEN_FORMAT_16
<< OXYGEN_REC_FORMAT_B_SHIFT
) |
411 (OXYGEN_FORMAT_16
<< OXYGEN_REC_FORMAT_C_SHIFT
));
412 oxygen_write8(chip
, OXYGEN_PLAY_FORMAT
,
413 (OXYGEN_FORMAT_16
<< OXYGEN_SPDIF_FORMAT_SHIFT
) |
414 (OXYGEN_FORMAT_16
<< OXYGEN_MULTICH_FORMAT_SHIFT
));
415 oxygen_write8(chip
, OXYGEN_REC_CHANNELS
, OXYGEN_REC_CHANNELS_2_2_2
);
416 oxygen_write16(chip
, OXYGEN_I2S_MULTICH_FORMAT
,
418 chip
->model
.dac_i2s_format
|
419 OXYGEN_I2S_MCLK(chip
->model
.dac_mclks
) |
423 if (chip
->model
.device_config
& CAPTURE_0_FROM_I2S_1
)
424 oxygen_write16(chip
, OXYGEN_I2S_A_FORMAT
,
426 chip
->model
.adc_i2s_format
|
427 OXYGEN_I2S_MCLK(chip
->model
.adc_mclks
) |
432 oxygen_write16(chip
, OXYGEN_I2S_A_FORMAT
,
434 OXYGEN_I2S_MUTE_MCLK
);
435 if (chip
->model
.device_config
& (CAPTURE_0_FROM_I2S_2
|
436 CAPTURE_2_FROM_I2S_2
))
437 oxygen_write16(chip
, OXYGEN_I2S_B_FORMAT
,
439 chip
->model
.adc_i2s_format
|
440 OXYGEN_I2S_MCLK(chip
->model
.adc_mclks
) |
445 oxygen_write16(chip
, OXYGEN_I2S_B_FORMAT
,
447 OXYGEN_I2S_MUTE_MCLK
);
448 oxygen_write16(chip
, OXYGEN_I2S_C_FORMAT
,
450 OXYGEN_I2S_MUTE_MCLK
);
451 oxygen_clear_bits32(chip
, OXYGEN_SPDIF_CONTROL
,
452 OXYGEN_SPDIF_OUT_ENABLE
|
453 OXYGEN_SPDIF_LOOPBACK
);
454 if (chip
->model
.device_config
& CAPTURE_1_FROM_SPDIF
)
455 oxygen_write32_masked(chip
, OXYGEN_SPDIF_CONTROL
,
456 OXYGEN_SPDIF_SENSE_MASK
|
457 OXYGEN_SPDIF_LOCK_MASK
|
458 OXYGEN_SPDIF_RATE_MASK
|
459 OXYGEN_SPDIF_LOCK_PAR
|
460 OXYGEN_SPDIF_IN_CLOCK_96
,
461 OXYGEN_SPDIF_SENSE_MASK
|
462 OXYGEN_SPDIF_LOCK_MASK
|
463 OXYGEN_SPDIF_RATE_MASK
|
464 OXYGEN_SPDIF_SENSE_PAR
|
465 OXYGEN_SPDIF_LOCK_PAR
|
466 OXYGEN_SPDIF_IN_CLOCK_MASK
);
468 oxygen_clear_bits32(chip
, OXYGEN_SPDIF_CONTROL
,
469 OXYGEN_SPDIF_SENSE_MASK
|
470 OXYGEN_SPDIF_LOCK_MASK
|
471 OXYGEN_SPDIF_RATE_MASK
);
472 oxygen_write32(chip
, OXYGEN_SPDIF_OUTPUT_BITS
, chip
->spdif_bits
);
473 oxygen_write16(chip
, OXYGEN_2WIRE_BUS_STATUS
,
474 OXYGEN_2WIRE_LENGTH_8
|
475 OXYGEN_2WIRE_INTERRUPT_MASK
|
476 OXYGEN_2WIRE_SPEED_STANDARD
);
477 oxygen_clear_bits8(chip
, OXYGEN_MPU401_CONTROL
, OXYGEN_MPU401_LOOPBACK
);
478 oxygen_write8(chip
, OXYGEN_GPI_INTERRUPT_MASK
, 0);
479 oxygen_write16(chip
, OXYGEN_GPIO_INTERRUPT_MASK
, 0);
480 oxygen_write16(chip
, OXYGEN_PLAY_ROUTING
,
481 OXYGEN_PLAY_MULTICH_I2S_DAC
|
482 OXYGEN_PLAY_SPDIF_SPDIF
|
483 (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT
) |
484 (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT
) |
485 (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT
) |
486 (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT
));
487 oxygen_write8(chip
, OXYGEN_REC_ROUTING
,
488 OXYGEN_REC_A_ROUTE_I2S_ADC_1
|
489 OXYGEN_REC_B_ROUTE_I2S_ADC_2
|
490 OXYGEN_REC_C_ROUTE_SPDIF
);
491 oxygen_write8(chip
, OXYGEN_ADC_MONITOR
, 0);
492 oxygen_write8(chip
, OXYGEN_A_MONITOR_ROUTING
,
493 (0 << OXYGEN_A_MONITOR_ROUTE_0_SHIFT
) |
494 (1 << OXYGEN_A_MONITOR_ROUTE_1_SHIFT
) |
495 (2 << OXYGEN_A_MONITOR_ROUTE_2_SHIFT
) |
496 (3 << OXYGEN_A_MONITOR_ROUTE_3_SHIFT
));
498 if (chip
->has_ac97_0
| chip
->has_ac97_1
)
499 oxygen_write8(chip
, OXYGEN_AC97_INTERRUPT_MASK
,
500 OXYGEN_AC97_INT_READ_DONE
|
501 OXYGEN_AC97_INT_WRITE_DONE
);
503 oxygen_write8(chip
, OXYGEN_AC97_INTERRUPT_MASK
, 0);
504 oxygen_write32(chip
, OXYGEN_AC97_OUT_CONFIG
, 0);
505 oxygen_write32(chip
, OXYGEN_AC97_IN_CONFIG
, 0);
506 if (!(chip
->has_ac97_0
| chip
->has_ac97_1
))
507 oxygen_set_bits16(chip
, OXYGEN_AC97_CONTROL
,
508 OXYGEN_AC97_CLOCK_DISABLE
);
509 if (!chip
->has_ac97_0
) {
510 oxygen_set_bits16(chip
, OXYGEN_AC97_CONTROL
,
511 OXYGEN_AC97_NO_CODEC_0
);
513 oxygen_write_ac97(chip
, 0, AC97_RESET
, 0);
515 oxygen_ac97_set_bits(chip
, 0, CM9780_GPIO_SETUP
,
516 CM9780_GPIO0IO
| CM9780_GPIO1IO
);
517 oxygen_ac97_set_bits(chip
, 0, CM9780_MIXER
,
518 CM9780_BSTSEL
| CM9780_STRO_MIC
|
519 CM9780_MIX2FR
| CM9780_PCBSW
);
520 oxygen_ac97_set_bits(chip
, 0, CM9780_JACK
,
521 CM9780_RSOE
| CM9780_CBOE
|
522 CM9780_SSOE
| CM9780_FROE
|
523 CM9780_MIC2MIC
| CM9780_LI2LI
);
524 oxygen_write_ac97(chip
, 0, AC97_MASTER
, 0x0000);
525 oxygen_write_ac97(chip
, 0, AC97_PC_BEEP
, 0x8000);
526 oxygen_write_ac97(chip
, 0, AC97_MIC
, 0x8808);
527 oxygen_write_ac97(chip
, 0, AC97_LINE
, 0x0808);
528 oxygen_write_ac97(chip
, 0, AC97_CD
, 0x8808);
529 oxygen_write_ac97(chip
, 0, AC97_VIDEO
, 0x8808);
530 oxygen_write_ac97(chip
, 0, AC97_AUX
, 0x8808);
531 oxygen_write_ac97(chip
, 0, AC97_REC_GAIN
, 0x8000);
532 oxygen_write_ac97(chip
, 0, AC97_CENTER_LFE_MASTER
, 0x8080);
533 oxygen_write_ac97(chip
, 0, AC97_SURROUND_MASTER
, 0x8080);
534 oxygen_ac97_clear_bits(chip
, 0, CM9780_GPIO_STATUS
,
536 /* power down unused ADCs and DACs */
537 oxygen_ac97_set_bits(chip
, 0, AC97_POWERDOWN
,
538 AC97_PD_PR0
| AC97_PD_PR1
);
539 oxygen_ac97_set_bits(chip
, 0, AC97_EXTENDED_STATUS
,
540 AC97_EA_PRI
| AC97_EA_PRJ
| AC97_EA_PRK
);
542 if (chip
->has_ac97_1
) {
543 oxygen_set_bits32(chip
, OXYGEN_AC97_OUT_CONFIG
,
544 OXYGEN_AC97_CODEC1_SLOT3
|
545 OXYGEN_AC97_CODEC1_SLOT4
);
546 oxygen_write_ac97(chip
, 1, AC97_RESET
, 0);
548 oxygen_write_ac97(chip
, 1, AC97_MASTER
, 0x0000);
549 oxygen_write_ac97(chip
, 1, AC97_HEADPHONE
, 0x8000);
550 oxygen_write_ac97(chip
, 1, AC97_PC_BEEP
, 0x8000);
551 oxygen_write_ac97(chip
, 1, AC97_MIC
, 0x8808);
552 oxygen_write_ac97(chip
, 1, AC97_LINE
, 0x8808);
553 oxygen_write_ac97(chip
, 1, AC97_CD
, 0x8808);
554 oxygen_write_ac97(chip
, 1, AC97_VIDEO
, 0x8808);
555 oxygen_write_ac97(chip
, 1, AC97_AUX
, 0x8808);
556 oxygen_write_ac97(chip
, 1, AC97_PCM
, 0x0808);
557 oxygen_write_ac97(chip
, 1, AC97_REC_SEL
, 0x0000);
558 oxygen_write_ac97(chip
, 1, AC97_REC_GAIN
, 0x0000);
559 oxygen_ac97_set_bits(chip
, 1, 0x6a, 0x0040);
563 static void oxygen_shutdown(struct oxygen
*chip
)
565 spin_lock_irq(&chip
->reg_lock
);
566 chip
->interrupt_mask
= 0;
567 chip
->pcm_running
= 0;
568 oxygen_write16(chip
, OXYGEN_DMA_STATUS
, 0);
569 oxygen_write16(chip
, OXYGEN_INTERRUPT_MASK
, 0);
570 spin_unlock_irq(&chip
->reg_lock
);
573 static void oxygen_card_free(struct snd_card
*card
)
575 struct oxygen
*chip
= card
->private_data
;
577 oxygen_shutdown(chip
);
579 free_irq(chip
->irq
, chip
);
580 flush_work_sync(&chip
->spdif_input_bits_work
);
581 flush_work_sync(&chip
->gpio_work
);
582 chip
->model
.cleanup(chip
);
583 kfree(chip
->model_data
);
584 mutex_destroy(&chip
->mutex
);
585 pci_release_regions(chip
->pci
);
586 pci_disable_device(chip
->pci
);
589 int oxygen_pci_probe(struct pci_dev
*pci
, int index
, char *id
,
590 struct module
*owner
,
591 const struct pci_device_id
*ids
,
592 int (*get_model
)(struct oxygen
*chip
,
593 const struct pci_device_id
*id
597 struct snd_card
*card
;
599 const struct pci_device_id
*pci_id
;
602 err
= snd_card_create(index
, id
, owner
, sizeof(*chip
), &card
);
606 chip
= card
->private_data
;
610 spin_lock_init(&chip
->reg_lock
);
611 mutex_init(&chip
->mutex
);
612 INIT_WORK(&chip
->spdif_input_bits_work
,
613 oxygen_spdif_input_bits_changed
);
614 INIT_WORK(&chip
->gpio_work
, oxygen_gpio_changed
);
615 init_waitqueue_head(&chip
->ac97_waitqueue
);
617 err
= pci_enable_device(pci
);
621 err
= pci_request_regions(pci
, DRIVER
);
623 snd_printk(KERN_ERR
"cannot reserve PCI resources\n");
627 if (!(pci_resource_flags(pci
, 0) & IORESOURCE_IO
) ||
628 pci_resource_len(pci
, 0) < OXYGEN_IO_SIZE
) {
629 snd_printk(KERN_ERR
"invalid PCI I/O range\n");
631 goto err_pci_regions
;
633 chip
->addr
= pci_resource_start(pci
, 0);
635 pci_id
= oxygen_search_pci_id(chip
, ids
);
638 goto err_pci_regions
;
640 oxygen_restore_eeprom(chip
, pci_id
);
641 err
= get_model(chip
, pci_id
);
643 goto err_pci_regions
;
645 if (chip
->model
.model_data_size
) {
646 chip
->model_data
= kzalloc(chip
->model
.model_data_size
,
648 if (!chip
->model_data
) {
650 goto err_pci_regions
;
655 snd_card_set_dev(card
, &pci
->dev
);
656 card
->private_free
= oxygen_card_free
;
658 configure_pcie_bridge(pci
);
660 chip
->model
.init(chip
);
662 err
= request_irq(pci
->irq
, oxygen_interrupt
, IRQF_SHARED
,
665 snd_printk(KERN_ERR
"cannot grab interrupt %d\n", pci
->irq
);
668 chip
->irq
= pci
->irq
;
670 strcpy(card
->driver
, chip
->model
.chip
);
671 strcpy(card
->shortname
, chip
->model
.shortname
);
672 sprintf(card
->longname
, "%s (rev %u) at %#lx, irq %i",
673 chip
->model
.longname
, chip
->revision
, chip
->addr
, chip
->irq
);
674 strcpy(card
->mixername
, chip
->model
.chip
);
675 snd_component_add(card
, chip
->model
.chip
);
677 err
= oxygen_pcm_init(chip
);
681 err
= oxygen_mixer_init(chip
);
685 if (chip
->model
.device_config
& (MIDI_OUTPUT
| MIDI_INPUT
)) {
686 unsigned int info_flags
= MPU401_INFO_INTEGRATED
;
687 if (chip
->model
.device_config
& MIDI_OUTPUT
)
688 info_flags
|= MPU401_INFO_OUTPUT
;
689 if (chip
->model
.device_config
& MIDI_INPUT
)
690 info_flags
|= MPU401_INFO_INPUT
;
691 err
= snd_mpu401_uart_new(card
, 0, MPU401_HW_CMIPCI
,
692 chip
->addr
+ OXYGEN_MPU401
,
699 oxygen_proc_init(chip
);
701 spin_lock_irq(&chip
->reg_lock
);
702 if (chip
->model
.device_config
& CAPTURE_1_FROM_SPDIF
)
703 chip
->interrupt_mask
|= OXYGEN_INT_SPDIF_IN_DETECT
;
704 if (chip
->has_ac97_0
| chip
->has_ac97_1
)
705 chip
->interrupt_mask
|= OXYGEN_INT_AC97
;
706 oxygen_write16(chip
, OXYGEN_INTERRUPT_MASK
, chip
->interrupt_mask
);
707 spin_unlock_irq(&chip
->reg_lock
);
709 err
= snd_card_register(card
);
713 pci_set_drvdata(pci
, card
);
717 pci_release_regions(pci
);
719 pci_disable_device(pci
);
724 EXPORT_SYMBOL(oxygen_pci_probe
);
726 void oxygen_pci_remove(struct pci_dev
*pci
)
728 snd_card_free(pci_get_drvdata(pci
));
729 pci_set_drvdata(pci
, NULL
);
731 EXPORT_SYMBOL(oxygen_pci_remove
);
734 int oxygen_pci_suspend(struct pci_dev
*pci
, pm_message_t state
)
736 struct snd_card
*card
= pci_get_drvdata(pci
);
737 struct oxygen
*chip
= card
->private_data
;
738 unsigned int i
, saved_interrupt_mask
;
740 snd_power_change_state(card
, SNDRV_CTL_POWER_D3hot
);
742 for (i
= 0; i
< PCM_COUNT
; ++i
)
743 if (chip
->streams
[i
])
744 snd_pcm_suspend(chip
->streams
[i
]);
746 if (chip
->model
.suspend
)
747 chip
->model
.suspend(chip
);
749 spin_lock_irq(&chip
->reg_lock
);
750 saved_interrupt_mask
= chip
->interrupt_mask
;
751 chip
->interrupt_mask
= 0;
752 oxygen_write16(chip
, OXYGEN_DMA_STATUS
, 0);
753 oxygen_write16(chip
, OXYGEN_INTERRUPT_MASK
, 0);
754 spin_unlock_irq(&chip
->reg_lock
);
756 synchronize_irq(chip
->irq
);
757 flush_work_sync(&chip
->spdif_input_bits_work
);
758 flush_work_sync(&chip
->gpio_work
);
759 chip
->interrupt_mask
= saved_interrupt_mask
;
761 pci_disable_device(pci
);
763 pci_set_power_state(pci
, pci_choose_state(pci
, state
));
766 EXPORT_SYMBOL(oxygen_pci_suspend
);
768 static const u32 registers_to_restore
[OXYGEN_IO_SIZE
/ 32] = {
769 0xffffffff, 0x00ff077f, 0x00011d08, 0x007f00ff,
770 0x00300000, 0x00000fe4, 0x0ff7001f, 0x00000000
772 static const u32 ac97_registers_to_restore
[2][0x40 / 32] = {
773 { 0x18284fa2, 0x03060000 },
774 { 0x00007fa6, 0x00200000 }
777 static inline int is_bit_set(const u32
*bitmap
, unsigned int bit
)
779 return bitmap
[bit
/ 32] & (1 << (bit
& 31));
782 static void oxygen_restore_ac97(struct oxygen
*chip
, unsigned int codec
)
786 oxygen_write_ac97(chip
, codec
, AC97_RESET
, 0);
788 for (i
= 1; i
< 0x40; ++i
)
789 if (is_bit_set(ac97_registers_to_restore
[codec
], i
))
790 oxygen_write_ac97(chip
, codec
, i
* 2,
791 chip
->saved_ac97_registers
[codec
][i
]);
794 int oxygen_pci_resume(struct pci_dev
*pci
)
796 struct snd_card
*card
= pci_get_drvdata(pci
);
797 struct oxygen
*chip
= card
->private_data
;
800 pci_set_power_state(pci
, PCI_D0
);
801 pci_restore_state(pci
);
802 if (pci_enable_device(pci
) < 0) {
803 snd_printk(KERN_ERR
"cannot reenable device");
804 snd_card_disconnect(card
);
809 oxygen_write16(chip
, OXYGEN_DMA_STATUS
, 0);
810 oxygen_write16(chip
, OXYGEN_INTERRUPT_MASK
, 0);
811 for (i
= 0; i
< OXYGEN_IO_SIZE
; ++i
)
812 if (is_bit_set(registers_to_restore
, i
))
813 oxygen_write8(chip
, i
, chip
->saved_registers
._8
[i
]);
814 if (chip
->has_ac97_0
)
815 oxygen_restore_ac97(chip
, 0);
816 if (chip
->has_ac97_1
)
817 oxygen_restore_ac97(chip
, 1);
819 if (chip
->model
.resume
)
820 chip
->model
.resume(chip
);
822 oxygen_write16(chip
, OXYGEN_INTERRUPT_MASK
, chip
->interrupt_mask
);
824 snd_power_change_state(card
, SNDRV_CTL_POWER_D0
);
827 EXPORT_SYMBOL(oxygen_pci_resume
);
828 #endif /* CONFIG_PM */
830 void oxygen_pci_shutdown(struct pci_dev
*pci
)
832 struct snd_card
*card
= pci_get_drvdata(pci
);
833 struct oxygen
*chip
= card
->private_data
;
835 oxygen_shutdown(chip
);
836 chip
->model
.cleanup(chip
);
838 EXPORT_SYMBOL(oxygen_pci_shutdown
);