Thomas Gleixner | 9c9cf6b | 2019-06-01 10:08:19 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 2 | /* |
| 3 | * C-Media CMI8788 driver - main driver module |
| 4 | * |
| 5 | * Copyright (c) Clemens Ladisch <clemens@ladisch.de> |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 8 | #include <linux/delay.h> |
| 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/mutex.h> |
| 11 | #include <linux/pci.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 13 | #include <linux/module.h> |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 14 | #include <sound/ac97_codec.h> |
| 15 | #include <sound/asoundef.h> |
| 16 | #include <sound/core.h> |
| 17 | #include <sound/info.h> |
| 18 | #include <sound/mpu401.h> |
| 19 | #include <sound/pcm.h> |
| 20 | #include "oxygen.h" |
Clemens Ladisch | 878ac3e | 2008-01-21 08:50:19 +0100 | [diff] [blame] | 21 | #include "cm9780.h" |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 22 | |
| 23 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); |
| 24 | MODULE_DESCRIPTION("C-Media CMI8788 helper library"); |
Clemens Ladisch | d023dc0 | 2008-05-13 09:18:27 +0200 | [diff] [blame] | 25 | MODULE_LICENSE("GPL v2"); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 26 | |
Clemens Ladisch | a69bb3c | 2009-02-19 08:38:55 +0100 | [diff] [blame] | 27 | #define DRIVER "oxygen" |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 28 | |
Clemens Ladisch | 397b1dc | 2008-09-22 09:04:43 +0200 | [diff] [blame] | 29 | static inline int oxygen_uart_input_ready(struct oxygen *chip) |
| 30 | { |
| 31 | return !(oxygen_read8(chip, OXYGEN_MPU401 + 1) & MPU401_RX_EMPTY); |
| 32 | } |
| 33 | |
| 34 | static void oxygen_read_uart(struct oxygen *chip) |
| 35 | { |
| 36 | if (unlikely(!oxygen_uart_input_ready(chip))) { |
| 37 | /* no data, but read it anyway to clear the interrupt */ |
| 38 | oxygen_read8(chip, OXYGEN_MPU401); |
| 39 | return; |
| 40 | } |
| 41 | do { |
| 42 | u8 data = oxygen_read8(chip, OXYGEN_MPU401); |
| 43 | if (data == MPU401_ACK) |
| 44 | continue; |
| 45 | if (chip->uart_input_count >= ARRAY_SIZE(chip->uart_input)) |
| 46 | chip->uart_input_count = 0; |
| 47 | chip->uart_input[chip->uart_input_count++] = data; |
| 48 | } while (oxygen_uart_input_ready(chip)); |
| 49 | if (chip->model.uart_input) |
| 50 | chip->model.uart_input(chip); |
| 51 | } |
| 52 | |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 53 | static irqreturn_t oxygen_interrupt(int dummy, void *dev_id) |
| 54 | { |
| 55 | struct oxygen *chip = dev_id; |
| 56 | unsigned int status, clear, elapsed_streams, i; |
| 57 | |
| 58 | status = oxygen_read16(chip, OXYGEN_INTERRUPT_STATUS); |
| 59 | if (!status) |
| 60 | return IRQ_NONE; |
| 61 | |
| 62 | spin_lock(&chip->reg_lock); |
| 63 | |
| 64 | clear = status & (OXYGEN_CHANNEL_A | |
| 65 | OXYGEN_CHANNEL_B | |
| 66 | OXYGEN_CHANNEL_C | |
| 67 | OXYGEN_CHANNEL_SPDIF | |
| 68 | OXYGEN_CHANNEL_MULTICH | |
| 69 | OXYGEN_CHANNEL_AC97 | |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 70 | OXYGEN_INT_SPDIF_IN_DETECT | |
Clemens Ladisch | 1e821dd | 2008-01-28 08:34:21 +0100 | [diff] [blame] | 71 | OXYGEN_INT_GPIO | |
| 72 | OXYGEN_INT_AC97); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 73 | if (clear) { |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 74 | if (clear & OXYGEN_INT_SPDIF_IN_DETECT) |
| 75 | chip->interrupt_mask &= ~OXYGEN_INT_SPDIF_IN_DETECT; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 76 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, |
| 77 | chip->interrupt_mask & ~clear); |
| 78 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, |
| 79 | chip->interrupt_mask); |
| 80 | } |
| 81 | |
| 82 | elapsed_streams = status & chip->pcm_running; |
| 83 | |
| 84 | spin_unlock(&chip->reg_lock); |
| 85 | |
| 86 | for (i = 0; i < PCM_COUNT; ++i) |
| 87 | if ((elapsed_streams & (1 << i)) && chip->streams[i]) |
| 88 | snd_pcm_period_elapsed(chip->streams[i]); |
| 89 | |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 90 | if (status & OXYGEN_INT_SPDIF_IN_DETECT) { |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 91 | spin_lock(&chip->reg_lock); |
| 92 | i = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL); |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame] | 93 | if (i & (OXYGEN_SPDIF_SENSE_INT | OXYGEN_SPDIF_LOCK_INT | |
| 94 | OXYGEN_SPDIF_RATE_INT)) { |
| 95 | /* write the interrupt bit(s) to clear */ |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 96 | oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, i); |
| 97 | schedule_work(&chip->spdif_input_bits_work); |
| 98 | } |
| 99 | spin_unlock(&chip->reg_lock); |
| 100 | } |
| 101 | |
| 102 | if (status & OXYGEN_INT_GPIO) |
Clemens Ladisch | 7c01415 | 2008-01-28 08:36:55 +0100 | [diff] [blame] | 103 | schedule_work(&chip->gpio_work); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 104 | |
Clemens Ladisch | 397b1dc | 2008-09-22 09:04:43 +0200 | [diff] [blame] | 105 | if (status & OXYGEN_INT_MIDI) { |
| 106 | if (chip->midi) |
| 107 | snd_mpu401_uart_interrupt(0, chip->midi->private_data); |
| 108 | else |
| 109 | oxygen_read_uart(chip); |
| 110 | } |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 111 | |
Clemens Ladisch | 1e821dd | 2008-01-28 08:34:21 +0100 | [diff] [blame] | 112 | if (status & OXYGEN_INT_AC97) |
| 113 | wake_up(&chip->ac97_waitqueue); |
| 114 | |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 115 | return IRQ_HANDLED; |
| 116 | } |
| 117 | |
| 118 | static void oxygen_spdif_input_bits_changed(struct work_struct *work) |
| 119 | { |
| 120 | struct oxygen *chip = container_of(work, struct oxygen, |
| 121 | spdif_input_bits_work); |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame] | 122 | u32 reg; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 123 | |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame] | 124 | /* |
| 125 | * This function gets called when there is new activity on the SPDIF |
| 126 | * input, or when we lose lock on the input signal, or when the rate |
| 127 | * changes. |
| 128 | */ |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 129 | msleep(1); |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame] | 130 | spin_lock_irq(&chip->reg_lock); |
| 131 | reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL); |
| 132 | if ((reg & (OXYGEN_SPDIF_SENSE_STATUS | |
| 133 | OXYGEN_SPDIF_LOCK_STATUS)) |
| 134 | == OXYGEN_SPDIF_SENSE_STATUS) { |
| 135 | /* |
| 136 | * If we detect activity on the SPDIF input but cannot lock to |
| 137 | * a signal, the clock bit is likely to be wrong. |
| 138 | */ |
| 139 | reg ^= OXYGEN_SPDIF_IN_CLOCK_MASK; |
| 140 | oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 141 | spin_unlock_irq(&chip->reg_lock); |
| 142 | msleep(1); |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame] | 143 | spin_lock_irq(&chip->reg_lock); |
| 144 | reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL); |
| 145 | if ((reg & (OXYGEN_SPDIF_SENSE_STATUS | |
| 146 | OXYGEN_SPDIF_LOCK_STATUS)) |
| 147 | == OXYGEN_SPDIF_SENSE_STATUS) { |
| 148 | /* nothing detected with either clock; give up */ |
| 149 | if ((reg & OXYGEN_SPDIF_IN_CLOCK_MASK) |
| 150 | == OXYGEN_SPDIF_IN_CLOCK_192) { |
| 151 | /* |
| 152 | * Reset clock to <= 96 kHz because this is |
| 153 | * more likely to be received next time. |
| 154 | */ |
| 155 | reg &= ~OXYGEN_SPDIF_IN_CLOCK_MASK; |
| 156 | reg |= OXYGEN_SPDIF_IN_CLOCK_96; |
| 157 | oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg); |
| 158 | } |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 159 | } |
| 160 | } |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame] | 161 | spin_unlock_irq(&chip->reg_lock); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 162 | |
Clemens Ladisch | 01a3aff | 2008-01-14 08:56:01 +0100 | [diff] [blame] | 163 | if (chip->controls[CONTROL_SPDIF_INPUT_BITS]) { |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 164 | spin_lock_irq(&chip->reg_lock); |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 165 | chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 166 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, |
| 167 | chip->interrupt_mask); |
| 168 | spin_unlock_irq(&chip->reg_lock); |
| 169 | |
Clemens Ladisch | 7f0b894 | 2008-01-21 08:53:30 +0100 | [diff] [blame] | 170 | /* |
| 171 | * We don't actually know that any channel status bits have |
| 172 | * changed, but let's send a notification just to be sure. |
| 173 | */ |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 174 | snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, |
Clemens Ladisch | 01a3aff | 2008-01-14 08:56:01 +0100 | [diff] [blame] | 175 | &chip->controls[CONTROL_SPDIF_INPUT_BITS]->id); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Clemens Ladisch | 7c01415 | 2008-01-28 08:36:55 +0100 | [diff] [blame] | 179 | static void oxygen_gpio_changed(struct work_struct *work) |
| 180 | { |
| 181 | struct oxygen *chip = container_of(work, struct oxygen, gpio_work); |
| 182 | |
Clemens Ladisch | 9bd6a73 | 2008-09-22 08:55:19 +0200 | [diff] [blame] | 183 | if (chip->model.gpio_changed) |
| 184 | chip->model.gpio_changed(chip); |
Clemens Ladisch | 7c01415 | 2008-01-28 08:36:55 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 187 | static void oxygen_proc_read(struct snd_info_entry *entry, |
| 188 | struct snd_info_buffer *buffer) |
| 189 | { |
| 190 | struct oxygen *chip = entry->private_data; |
| 191 | int i, j; |
| 192 | |
Clemens Ladisch | e2943ef | 2010-12-02 11:42:00 +0100 | [diff] [blame] | 193 | switch (oxygen_read8(chip, OXYGEN_REVISION) & OXYGEN_PACKAGE_ID_MASK) { |
| 194 | case OXYGEN_PACKAGE_ID_8786: i = '6'; break; |
| 195 | case OXYGEN_PACKAGE_ID_8787: i = '7'; break; |
| 196 | case OXYGEN_PACKAGE_ID_8788: i = '8'; break; |
| 197 | default: i = '?'; break; |
| 198 | } |
| 199 | snd_iprintf(buffer, "CMI878%c:\n", i); |
Clemens Ladisch | c136500 | 2008-05-13 09:19:53 +0200 | [diff] [blame] | 200 | for (i = 0; i < OXYGEN_IO_SIZE; i += 0x10) { |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 201 | snd_iprintf(buffer, "%02x:", i); |
| 202 | for (j = 0; j < 0x10; ++j) |
| 203 | snd_iprintf(buffer, " %02x", oxygen_read8(chip, i + j)); |
| 204 | snd_iprintf(buffer, "\n"); |
| 205 | } |
| 206 | if (mutex_lock_interruptible(&chip->mutex) < 0) |
| 207 | return; |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 208 | if (chip->has_ac97_0) { |
Clemens Ladisch | 9719fca | 2010-12-02 11:41:10 +0100 | [diff] [blame] | 209 | snd_iprintf(buffer, "\nAC97:\n"); |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 210 | for (i = 0; i < 0x80; i += 0x10) { |
| 211 | snd_iprintf(buffer, "%02x:", i); |
| 212 | for (j = 0; j < 0x10; j += 2) |
| 213 | snd_iprintf(buffer, " %04x", |
| 214 | oxygen_read_ac97(chip, 0, i + j)); |
| 215 | snd_iprintf(buffer, "\n"); |
| 216 | } |
| 217 | } |
| 218 | if (chip->has_ac97_1) { |
Clemens Ladisch | 9719fca | 2010-12-02 11:41:10 +0100 | [diff] [blame] | 219 | snd_iprintf(buffer, "\nAC97 2:\n"); |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 220 | for (i = 0; i < 0x80; i += 0x10) { |
| 221 | snd_iprintf(buffer, "%02x:", i); |
| 222 | for (j = 0; j < 0x10; j += 2) |
| 223 | snd_iprintf(buffer, " %04x", |
| 224 | oxygen_read_ac97(chip, 1, i + j)); |
| 225 | snd_iprintf(buffer, "\n"); |
| 226 | } |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 227 | } |
| 228 | mutex_unlock(&chip->mutex); |
Clemens Ladisch | 9719fca | 2010-12-02 11:41:10 +0100 | [diff] [blame] | 229 | if (chip->model.dump_registers) |
| 230 | chip->model.dump_registers(chip, buffer); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Takashi Iwai | f007dc0 | 2008-02-22 18:35:22 +0100 | [diff] [blame] | 233 | static void oxygen_proc_init(struct oxygen *chip) |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 234 | { |
Takashi Iwai | 47f2769 | 2019-02-04 16:01:39 +0100 | [diff] [blame] | 235 | snd_card_ro_proc_new(chip->card, "oxygen", chip, oxygen_proc_read); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 236 | } |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 237 | |
Clemens Ladisch | 30459d7 | 2009-02-19 08:42:44 +0100 | [diff] [blame] | 238 | static const struct pci_device_id * |
| 239 | oxygen_search_pci_id(struct oxygen *chip, const struct pci_device_id ids[]) |
| 240 | { |
| 241 | u16 subdevice; |
| 242 | |
| 243 | /* |
| 244 | * Make sure the EEPROM pins are available, i.e., not used for SPI. |
| 245 | * (This function is called before we initialize or use SPI.) |
| 246 | */ |
| 247 | oxygen_clear_bits8(chip, OXYGEN_FUNCTION, |
| 248 | OXYGEN_FUNCTION_ENABLE_SPI_4_5); |
| 249 | /* |
| 250 | * Read the subsystem device ID directly from the EEPROM, because the |
| 251 | * chip didn't if the first EEPROM word was overwritten. |
| 252 | */ |
| 253 | subdevice = oxygen_read_eeprom(chip, 2); |
Clemens Ladisch | 9265345 | 2009-09-02 18:25:39 +0200 | [diff] [blame] | 254 | /* use default ID if EEPROM is missing */ |
Clemens Ladisch | 31f86ba | 2010-11-02 17:18:23 +0100 | [diff] [blame] | 255 | if (subdevice == 0xffff && oxygen_read_eeprom(chip, 1) == 0xffff) |
Clemens Ladisch | 9265345 | 2009-09-02 18:25:39 +0200 | [diff] [blame] | 256 | subdevice = 0x8788; |
Clemens Ladisch | 30459d7 | 2009-02-19 08:42:44 +0100 | [diff] [blame] | 257 | /* |
| 258 | * We use only the subsystem device ID for searching because it is |
| 259 | * unique even without the subsystem vendor ID, which may have been |
| 260 | * overwritten in the EEPROM. |
| 261 | */ |
| 262 | for (; ids->vendor; ++ids) |
| 263 | if (ids->subdevice == subdevice && |
| 264 | ids->driver_data != BROKEN_EEPROM_DRIVER_DATA) |
| 265 | return ids; |
| 266 | return NULL; |
| 267 | } |
| 268 | |
Clemens Ladisch | 1275d6f | 2009-02-19 08:44:12 +0100 | [diff] [blame] | 269 | static void oxygen_restore_eeprom(struct oxygen *chip, |
| 270 | const struct pci_device_id *id) |
| 271 | { |
Clemens Ladisch | 87b6190 | 2009-09-28 11:05:18 +0200 | [diff] [blame] | 272 | u16 eeprom_id; |
| 273 | |
| 274 | eeprom_id = oxygen_read_eeprom(chip, 0); |
| 275 | if (eeprom_id != OXYGEN_EEPROM_ID && |
| 276 | (eeprom_id != 0xffff || id->subdevice != 0x8788)) { |
Clemens Ladisch | 1275d6f | 2009-02-19 08:44:12 +0100 | [diff] [blame] | 277 | /* |
| 278 | * This function gets called only when a known card model has |
| 279 | * been detected, i.e., we know there is a valid subsystem |
| 280 | * product ID at index 2 in the EEPROM. Therefore, we have |
| 281 | * been able to deduce the correct subsystem vendor ID, and |
| 282 | * this is enough information to restore the original EEPROM |
| 283 | * contents. |
| 284 | */ |
| 285 | oxygen_write_eeprom(chip, 1, id->subvendor); |
| 286 | oxygen_write_eeprom(chip, 0, OXYGEN_EEPROM_ID); |
| 287 | |
| 288 | oxygen_set_bits8(chip, OXYGEN_MISC, |
| 289 | OXYGEN_MISC_WRITE_PCI_SUBID); |
| 290 | pci_write_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, |
| 291 | id->subvendor); |
| 292 | pci_write_config_word(chip->pci, PCI_SUBSYSTEM_ID, |
| 293 | id->subdevice); |
| 294 | oxygen_clear_bits8(chip, OXYGEN_MISC, |
| 295 | OXYGEN_MISC_WRITE_PCI_SUBID); |
| 296 | |
Takashi Iwai | 03d3ac2 | 2014-02-25 18:00:26 +0100 | [diff] [blame] | 297 | dev_info(chip->card->dev, "EEPROM ID restored\n"); |
Clemens Ladisch | 1275d6f | 2009-02-19 08:44:12 +0100 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Clemens Ladisch | 7cb4ced | 2010-10-06 10:57:50 +0200 | [diff] [blame] | 301 | static void configure_pcie_bridge(struct pci_dev *pci) |
Clemens Ladisch | 362bc24 | 2009-09-28 11:05:58 +0200 | [diff] [blame] | 302 | { |
Clemens Ladisch | a5a3353 | 2015-01-16 22:15:43 +0100 | [diff] [blame] | 303 | enum { PEX811X, PI7C9X110, XIO2001 }; |
Clemens Ladisch | 7cb4ced | 2010-10-06 10:57:50 +0200 | [diff] [blame] | 304 | static const struct pci_device_id bridge_ids[] = { |
Clemens Ladisch | ebebeec | 2010-10-06 10:58:50 +0200 | [diff] [blame] | 305 | { PCI_VDEVICE(PLX, 0x8111), .driver_data = PEX811X }, |
| 306 | { PCI_VDEVICE(PLX, 0x8112), .driver_data = PEX811X }, |
Clemens Ladisch | 7cb4ced | 2010-10-06 10:57:50 +0200 | [diff] [blame] | 307 | { PCI_DEVICE(0x12d8, 0xe110), .driver_data = PI7C9X110 }, |
Clemens Ladisch | a5a3353 | 2015-01-16 22:15:43 +0100 | [diff] [blame] | 308 | { PCI_VDEVICE(TI, 0x8240), .driver_data = XIO2001 }, |
Clemens Ladisch | 7cb4ced | 2010-10-06 10:57:50 +0200 | [diff] [blame] | 309 | { } |
| 310 | }; |
| 311 | struct pci_dev *bridge; |
| 312 | const struct pci_device_id *id; |
Clemens Ladisch | 362bc24 | 2009-09-28 11:05:58 +0200 | [diff] [blame] | 313 | u32 tmp; |
| 314 | |
Clemens Ladisch | 7cb4ced | 2010-10-06 10:57:50 +0200 | [diff] [blame] | 315 | if (!pci->bus || !pci->bus->self) |
| 316 | return; |
| 317 | bridge = pci->bus->self; |
| 318 | |
| 319 | id = pci_match_id(bridge_ids, bridge); |
| 320 | if (!id) |
| 321 | return; |
| 322 | |
| 323 | switch (id->driver_data) { |
Clemens Ladisch | ebebeec | 2010-10-06 10:58:50 +0200 | [diff] [blame] | 324 | case PEX811X: /* PLX PEX8111/PEX8112 PCIe/PCI bridge */ |
| 325 | pci_read_config_dword(bridge, 0x48, &tmp); |
| 326 | tmp |= 1; /* enable blind prefetching */ |
| 327 | tmp |= 1 << 11; /* enable beacon generation */ |
| 328 | pci_write_config_dword(bridge, 0x48, tmp); |
| 329 | |
| 330 | pci_write_config_dword(bridge, 0x84, 0x0c); |
| 331 | pci_read_config_dword(bridge, 0x88, &tmp); |
| 332 | tmp &= ~(7 << 27); |
| 333 | tmp |= 2 << 27; /* set prefetch size to 128 bytes */ |
| 334 | pci_write_config_dword(bridge, 0x88, tmp); |
| 335 | break; |
| 336 | |
Clemens Ladisch | 7cb4ced | 2010-10-06 10:57:50 +0200 | [diff] [blame] | 337 | case PI7C9X110: /* Pericom PI7C9X110 PCIe/PCI bridge */ |
| 338 | pci_read_config_dword(bridge, 0x40, &tmp); |
| 339 | tmp |= 1; /* park the PCI arbiter to the sound chip */ |
| 340 | pci_write_config_dword(bridge, 0x40, tmp); |
| 341 | break; |
Clemens Ladisch | a5a3353 | 2015-01-16 22:15:43 +0100 | [diff] [blame] | 342 | |
| 343 | case XIO2001: /* Texas Instruments XIO2001 PCIe/PCI bridge */ |
| 344 | pci_read_config_dword(bridge, 0xe8, &tmp); |
| 345 | tmp &= ~0xf; /* request length limit: 64 bytes */ |
| 346 | tmp &= ~(0xf << 8); |
| 347 | tmp |= 1 << 8; /* request count limit: one buffer */ |
| 348 | pci_write_config_dword(bridge, 0xe8, tmp); |
| 349 | break; |
Clemens Ladisch | 362bc24 | 2009-09-28 11:05:58 +0200 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Takashi Iwai | f007dc0 | 2008-02-22 18:35:22 +0100 | [diff] [blame] | 353 | static void oxygen_init(struct oxygen *chip) |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 354 | { |
| 355 | unsigned int i; |
| 356 | |
| 357 | chip->dac_routing = 1; |
| 358 | for (i = 0; i < 8; ++i) |
Clemens Ladisch | 9bd6a73 | 2008-09-22 08:55:19 +0200 | [diff] [blame] | 359 | chip->dac_volume[i] = chip->model.dac_volume_min; |
Clemens Ladisch | e983532 | 2008-04-16 09:14:30 +0200 | [diff] [blame] | 360 | chip->dac_mute = 1; |
Tom Yan | 4bccb40 | 2018-12-21 20:52:57 +0800 | [diff] [blame] | 361 | chip->spdif_playback_enable = 0; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 362 | chip->spdif_bits = OXYGEN_SPDIF_C | OXYGEN_SPDIF_ORIGINAL | |
| 363 | (IEC958_AES1_CON_PCM_CODER << OXYGEN_SPDIF_CATEGORY_SHIFT); |
| 364 | chip->spdif_pcm_bits = chip->spdif_bits; |
| 365 | |
Clemens Ladisch | a1f80fc | 2011-01-10 16:36:23 +0100 | [diff] [blame] | 366 | if (!(oxygen_read8(chip, OXYGEN_REVISION) & OXYGEN_REVISION_2)) |
Clemens Ladisch | c2353a0 | 2008-01-18 09:17:53 +0100 | [diff] [blame] | 367 | oxygen_set_bits8(chip, OXYGEN_MISC, |
| 368 | OXYGEN_MISC_PCI_MEM_W_1_CLOCK); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 369 | |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 370 | i = oxygen_read16(chip, OXYGEN_AC97_CONTROL); |
| 371 | chip->has_ac97_0 = (i & OXYGEN_AC97_CODEC_0) != 0; |
| 372 | chip->has_ac97_1 = (i & OXYGEN_AC97_CODEC_1) != 0; |
| 373 | |
Clemens Ladisch | b78e3db | 2008-01-25 08:39:26 +0100 | [diff] [blame] | 374 | oxygen_write8_masked(chip, OXYGEN_FUNCTION, |
Clemens Ladisch | 87eedd2 | 2008-03-19 08:20:13 +0100 | [diff] [blame] | 375 | OXYGEN_FUNCTION_RESET_CODEC | |
Clemens Ladisch | 9bd6a73 | 2008-09-22 08:55:19 +0200 | [diff] [blame] | 376 | chip->model.function_flags, |
Clemens Ladisch | 87eedd2 | 2008-03-19 08:20:13 +0100 | [diff] [blame] | 377 | OXYGEN_FUNCTION_RESET_CODEC | |
| 378 | OXYGEN_FUNCTION_2WIRE_SPI_MASK | |
| 379 | OXYGEN_FUNCTION_ENABLE_SPI_4_5); |
Clemens Ladisch | b78e3db | 2008-01-25 08:39:26 +0100 | [diff] [blame] | 380 | oxygen_write8(chip, OXYGEN_DMA_STATUS, 0); |
| 381 | oxygen_write8(chip, OXYGEN_DMA_PAUSE, 0); |
| 382 | oxygen_write8(chip, OXYGEN_PLAY_CHANNELS, |
| 383 | OXYGEN_PLAY_CHANNELS_2 | |
| 384 | OXYGEN_DMA_A_BURST_8 | |
| 385 | OXYGEN_DMA_MULTICH_BURST_8); |
| 386 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0); |
Clemens Ladisch | db12b8e | 2008-03-19 08:20:59 +0100 | [diff] [blame] | 387 | oxygen_write8_masked(chip, OXYGEN_MISC, |
Clemens Ladisch | 9bd6a73 | 2008-09-22 08:55:19 +0200 | [diff] [blame] | 388 | chip->model.misc_flags, |
Clemens Ladisch | b78e3db | 2008-01-25 08:39:26 +0100 | [diff] [blame] | 389 | OXYGEN_MISC_WRITE_PCI_SUBID | |
| 390 | OXYGEN_MISC_REC_C_FROM_SPDIF | |
| 391 | OXYGEN_MISC_REC_B_FROM_AC97 | |
Clemens Ladisch | db12b8e | 2008-03-19 08:20:59 +0100 | [diff] [blame] | 392 | OXYGEN_MISC_REC_A_FROM_MULTICH | |
| 393 | OXYGEN_MISC_MIDI); |
Clemens Ladisch | b78e3db | 2008-01-25 08:39:26 +0100 | [diff] [blame] | 394 | oxygen_write8(chip, OXYGEN_REC_FORMAT, |
| 395 | (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_A_SHIFT) | |
| 396 | (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_B_SHIFT) | |
| 397 | (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_C_SHIFT)); |
| 398 | oxygen_write8(chip, OXYGEN_PLAY_FORMAT, |
| 399 | (OXYGEN_FORMAT_16 << OXYGEN_SPDIF_FORMAT_SHIFT) | |
| 400 | (OXYGEN_FORMAT_16 << OXYGEN_MULTICH_FORMAT_SHIFT)); |
| 401 | oxygen_write8(chip, OXYGEN_REC_CHANNELS, OXYGEN_REC_CHANNELS_2_2_2); |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 402 | oxygen_write16(chip, OXYGEN_I2S_MULTICH_FORMAT, |
Clemens Ladisch | 5b8bf2a | 2011-01-10 16:14:52 +0100 | [diff] [blame] | 403 | OXYGEN_RATE_48000 | |
| 404 | chip->model.dac_i2s_format | |
| 405 | OXYGEN_I2S_MCLK(chip->model.dac_mclks) | |
| 406 | OXYGEN_I2S_BITS_16 | |
| 407 | OXYGEN_I2S_MASTER | |
| 408 | OXYGEN_I2S_BCLK_64); |
Clemens Ladisch | d76596b | 2008-09-22 09:02:08 +0200 | [diff] [blame] | 409 | if (chip->model.device_config & CAPTURE_0_FROM_I2S_1) |
Clemens Ladisch | 43dd89c | 2008-03-19 08:21:32 +0100 | [diff] [blame] | 410 | oxygen_write16(chip, OXYGEN_I2S_A_FORMAT, |
Clemens Ladisch | 5b8bf2a | 2011-01-10 16:14:52 +0100 | [diff] [blame] | 411 | OXYGEN_RATE_48000 | |
| 412 | chip->model.adc_i2s_format | |
| 413 | OXYGEN_I2S_MCLK(chip->model.adc_mclks) | |
| 414 | OXYGEN_I2S_BITS_16 | |
| 415 | OXYGEN_I2S_MASTER | |
| 416 | OXYGEN_I2S_BCLK_64); |
Clemens Ladisch | 43dd89c | 2008-03-19 08:21:32 +0100 | [diff] [blame] | 417 | else |
| 418 | oxygen_write16(chip, OXYGEN_I2S_A_FORMAT, |
Clemens Ladisch | 5b8bf2a | 2011-01-10 16:14:52 +0100 | [diff] [blame] | 419 | OXYGEN_I2S_MASTER | |
| 420 | OXYGEN_I2S_MUTE_MCLK); |
Clemens Ladisch | d76596b | 2008-09-22 09:02:08 +0200 | [diff] [blame] | 421 | if (chip->model.device_config & (CAPTURE_0_FROM_I2S_2 | |
| 422 | CAPTURE_2_FROM_I2S_2)) |
Clemens Ladisch | 43dd89c | 2008-03-19 08:21:32 +0100 | [diff] [blame] | 423 | oxygen_write16(chip, OXYGEN_I2S_B_FORMAT, |
Clemens Ladisch | 5b8bf2a | 2011-01-10 16:14:52 +0100 | [diff] [blame] | 424 | OXYGEN_RATE_48000 | |
| 425 | chip->model.adc_i2s_format | |
| 426 | OXYGEN_I2S_MCLK(chip->model.adc_mclks) | |
| 427 | OXYGEN_I2S_BITS_16 | |
| 428 | OXYGEN_I2S_MASTER | |
| 429 | OXYGEN_I2S_BCLK_64); |
Clemens Ladisch | 43dd89c | 2008-03-19 08:21:32 +0100 | [diff] [blame] | 430 | else |
| 431 | oxygen_write16(chip, OXYGEN_I2S_B_FORMAT, |
Clemens Ladisch | 5b8bf2a | 2011-01-10 16:14:52 +0100 | [diff] [blame] | 432 | OXYGEN_I2S_MASTER | |
| 433 | OXYGEN_I2S_MUTE_MCLK); |
Clemens Ladisch | 0902fbb | 2015-01-16 22:15:13 +0100 | [diff] [blame] | 434 | if (chip->model.device_config & CAPTURE_3_FROM_I2S_3) |
| 435 | oxygen_write16(chip, OXYGEN_I2S_C_FORMAT, |
| 436 | OXYGEN_RATE_48000 | |
| 437 | chip->model.adc_i2s_format | |
| 438 | OXYGEN_I2S_MCLK(chip->model.adc_mclks) | |
| 439 | OXYGEN_I2S_BITS_16 | |
| 440 | OXYGEN_I2S_MASTER | |
| 441 | OXYGEN_I2S_BCLK_64); |
| 442 | else |
| 443 | oxygen_write16(chip, OXYGEN_I2S_C_FORMAT, |
| 444 | OXYGEN_I2S_MASTER | |
| 445 | OXYGEN_I2S_MUTE_MCLK); |
Clemens Ladisch | 1d98c7d | 2008-04-09 09:16:33 +0200 | [diff] [blame] | 446 | oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL, |
| 447 | OXYGEN_SPDIF_OUT_ENABLE | |
| 448 | OXYGEN_SPDIF_LOOPBACK); |
Clemens Ladisch | d76596b | 2008-09-22 09:02:08 +0200 | [diff] [blame] | 449 | if (chip->model.device_config & CAPTURE_1_FROM_SPDIF) |
Clemens Ladisch | 1d98c7d | 2008-04-09 09:16:33 +0200 | [diff] [blame] | 450 | oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL, |
| 451 | OXYGEN_SPDIF_SENSE_MASK | |
| 452 | OXYGEN_SPDIF_LOCK_MASK | |
| 453 | OXYGEN_SPDIF_RATE_MASK | |
| 454 | OXYGEN_SPDIF_LOCK_PAR | |
| 455 | OXYGEN_SPDIF_IN_CLOCK_96, |
| 456 | OXYGEN_SPDIF_SENSE_MASK | |
| 457 | OXYGEN_SPDIF_LOCK_MASK | |
| 458 | OXYGEN_SPDIF_RATE_MASK | |
| 459 | OXYGEN_SPDIF_SENSE_PAR | |
| 460 | OXYGEN_SPDIF_LOCK_PAR | |
| 461 | OXYGEN_SPDIF_IN_CLOCK_MASK); |
| 462 | else |
| 463 | oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL, |
| 464 | OXYGEN_SPDIF_SENSE_MASK | |
| 465 | OXYGEN_SPDIF_LOCK_MASK | |
| 466 | OXYGEN_SPDIF_RATE_MASK); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 467 | oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS, chip->spdif_bits); |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 468 | oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS, |
| 469 | OXYGEN_2WIRE_LENGTH_8 | |
| 470 | OXYGEN_2WIRE_INTERRUPT_MASK | |
| 471 | OXYGEN_2WIRE_SPEED_STANDARD); |
Clemens Ladisch | b78e3db | 2008-01-25 08:39:26 +0100 | [diff] [blame] | 472 | oxygen_clear_bits8(chip, OXYGEN_MPU401_CONTROL, OXYGEN_MPU401_LOOPBACK); |
| 473 | oxygen_write8(chip, OXYGEN_GPI_INTERRUPT_MASK, 0); |
| 474 | oxygen_write16(chip, OXYGEN_GPIO_INTERRUPT_MASK, 0); |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 475 | oxygen_write16(chip, OXYGEN_PLAY_ROUTING, |
Clemens Ladisch | b78e3db | 2008-01-25 08:39:26 +0100 | [diff] [blame] | 476 | OXYGEN_PLAY_MULTICH_I2S_DAC | |
| 477 | OXYGEN_PLAY_SPDIF_SPDIF | |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 478 | (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) | |
| 479 | (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) | |
| 480 | (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) | |
| 481 | (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT)); |
| 482 | oxygen_write8(chip, OXYGEN_REC_ROUTING, |
| 483 | OXYGEN_REC_A_ROUTE_I2S_ADC_1 | |
Clemens Ladisch | b78e3db | 2008-01-25 08:39:26 +0100 | [diff] [blame] | 484 | OXYGEN_REC_B_ROUTE_I2S_ADC_2 | |
Clemens Ladisch | c9946b2 | 2008-01-21 08:44:24 +0100 | [diff] [blame] | 485 | OXYGEN_REC_C_ROUTE_SPDIF); |
| 486 | oxygen_write8(chip, OXYGEN_ADC_MONITOR, 0); |
| 487 | oxygen_write8(chip, OXYGEN_A_MONITOR_ROUTING, |
| 488 | (0 << OXYGEN_A_MONITOR_ROUTE_0_SHIFT) | |
| 489 | (1 << OXYGEN_A_MONITOR_ROUTE_1_SHIFT) | |
| 490 | (2 << OXYGEN_A_MONITOR_ROUTE_2_SHIFT) | |
| 491 | (3 << OXYGEN_A_MONITOR_ROUTE_3_SHIFT)); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 492 | |
Clemens Ladisch | 1d98c7d | 2008-04-09 09:16:33 +0200 | [diff] [blame] | 493 | if (chip->has_ac97_0 | chip->has_ac97_1) |
| 494 | oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK, |
| 495 | OXYGEN_AC97_INT_READ_DONE | |
| 496 | OXYGEN_AC97_INT_WRITE_DONE); |
| 497 | else |
| 498 | oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK, 0); |
Clemens Ladisch | b78e3db | 2008-01-25 08:39:26 +0100 | [diff] [blame] | 499 | oxygen_write32(chip, OXYGEN_AC97_OUT_CONFIG, 0); |
| 500 | oxygen_write32(chip, OXYGEN_AC97_IN_CONFIG, 0); |
| 501 | if (!(chip->has_ac97_0 | chip->has_ac97_1)) |
| 502 | oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL, |
| 503 | OXYGEN_AC97_CLOCK_DISABLE); |
| 504 | if (!chip->has_ac97_0) { |
| 505 | oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL, |
| 506 | OXYGEN_AC97_NO_CODEC_0); |
| 507 | } else { |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 508 | oxygen_write_ac97(chip, 0, AC97_RESET, 0); |
| 509 | msleep(1); |
Clemens Ladisch | 878ac3e | 2008-01-21 08:50:19 +0100 | [diff] [blame] | 510 | oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_SETUP, |
| 511 | CM9780_GPIO0IO | CM9780_GPIO1IO); |
| 512 | oxygen_ac97_set_bits(chip, 0, CM9780_MIXER, |
| 513 | CM9780_BSTSEL | CM9780_STRO_MIC | |
| 514 | CM9780_MIX2FR | CM9780_PCBSW); |
| 515 | oxygen_ac97_set_bits(chip, 0, CM9780_JACK, |
| 516 | CM9780_RSOE | CM9780_CBOE | |
| 517 | CM9780_SSOE | CM9780_FROE | |
| 518 | CM9780_MIC2MIC | CM9780_LI2LI); |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 519 | oxygen_write_ac97(chip, 0, AC97_MASTER, 0x0000); |
| 520 | oxygen_write_ac97(chip, 0, AC97_PC_BEEP, 0x8000); |
| 521 | oxygen_write_ac97(chip, 0, AC97_MIC, 0x8808); |
| 522 | oxygen_write_ac97(chip, 0, AC97_LINE, 0x0808); |
| 523 | oxygen_write_ac97(chip, 0, AC97_CD, 0x8808); |
| 524 | oxygen_write_ac97(chip, 0, AC97_VIDEO, 0x8808); |
| 525 | oxygen_write_ac97(chip, 0, AC97_AUX, 0x8808); |
| 526 | oxygen_write_ac97(chip, 0, AC97_REC_GAIN, 0x8000); |
| 527 | oxygen_write_ac97(chip, 0, AC97_CENTER_LFE_MASTER, 0x8080); |
| 528 | oxygen_write_ac97(chip, 0, AC97_SURROUND_MASTER, 0x8080); |
Clemens Ladisch | e97f799 | 2008-04-01 10:02:18 +0200 | [diff] [blame] | 529 | oxygen_ac97_clear_bits(chip, 0, CM9780_GPIO_STATUS, |
| 530 | CM9780_GPO0); |
Clemens Ladisch | 31c7764 | 2008-01-16 08:28:17 +0100 | [diff] [blame] | 531 | /* power down unused ADCs and DACs */ |
| 532 | oxygen_ac97_set_bits(chip, 0, AC97_POWERDOWN, |
| 533 | AC97_PD_PR0 | AC97_PD_PR1); |
| 534 | oxygen_ac97_set_bits(chip, 0, AC97_EXTENDED_STATUS, |
| 535 | AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK); |
| 536 | } |
Clemens Ladisch | b78e3db | 2008-01-25 08:39:26 +0100 | [diff] [blame] | 537 | if (chip->has_ac97_1) { |
| 538 | oxygen_set_bits32(chip, OXYGEN_AC97_OUT_CONFIG, |
| 539 | OXYGEN_AC97_CODEC1_SLOT3 | |
| 540 | OXYGEN_AC97_CODEC1_SLOT4); |
| 541 | oxygen_write_ac97(chip, 1, AC97_RESET, 0); |
| 542 | msleep(1); |
| 543 | oxygen_write_ac97(chip, 1, AC97_MASTER, 0x0000); |
| 544 | oxygen_write_ac97(chip, 1, AC97_HEADPHONE, 0x8000); |
| 545 | oxygen_write_ac97(chip, 1, AC97_PC_BEEP, 0x8000); |
| 546 | oxygen_write_ac97(chip, 1, AC97_MIC, 0x8808); |
| 547 | oxygen_write_ac97(chip, 1, AC97_LINE, 0x8808); |
| 548 | oxygen_write_ac97(chip, 1, AC97_CD, 0x8808); |
| 549 | oxygen_write_ac97(chip, 1, AC97_VIDEO, 0x8808); |
| 550 | oxygen_write_ac97(chip, 1, AC97_AUX, 0x8808); |
| 551 | oxygen_write_ac97(chip, 1, AC97_PCM, 0x0808); |
| 552 | oxygen_write_ac97(chip, 1, AC97_REC_SEL, 0x0000); |
Clemens Ladisch | a360156 | 2008-01-28 08:35:20 +0100 | [diff] [blame] | 553 | oxygen_write_ac97(chip, 1, AC97_REC_GAIN, 0x0000); |
Clemens Ladisch | b78e3db | 2008-01-25 08:39:26 +0100 | [diff] [blame] | 554 | oxygen_ac97_set_bits(chip, 1, 0x6a, 0x0040); |
| 555 | } |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Clemens Ladisch | 4c25b93 | 2010-09-07 13:37:10 +0200 | [diff] [blame] | 558 | static void oxygen_shutdown(struct oxygen *chip) |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 559 | { |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 560 | spin_lock_irq(&chip->reg_lock); |
| 561 | chip->interrupt_mask = 0; |
| 562 | chip->pcm_running = 0; |
| 563 | oxygen_write16(chip, OXYGEN_DMA_STATUS, 0); |
| 564 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0); |
| 565 | spin_unlock_irq(&chip->reg_lock); |
Clemens Ladisch | 4c25b93 | 2010-09-07 13:37:10 +0200 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static void oxygen_card_free(struct snd_card *card) |
| 569 | { |
| 570 | struct oxygen *chip = card->private_data; |
| 571 | |
| 572 | oxygen_shutdown(chip); |
Jeff Garzik | f000fd8 | 2008-04-22 13:50:34 +0200 | [diff] [blame] | 573 | if (chip->irq >= 0) |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 574 | free_irq(chip->irq, chip); |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 575 | flush_work(&chip->spdif_input_bits_work); |
| 576 | flush_work(&chip->gpio_work); |
Clemens Ladisch | 9bd6a73 | 2008-09-22 08:55:19 +0200 | [diff] [blame] | 577 | chip->model.cleanup(chip); |
Clemens Ladisch | 6ed9115 | 2009-02-19 08:38:25 +0100 | [diff] [blame] | 578 | kfree(chip->model_data); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 579 | mutex_destroy(&chip->mutex); |
| 580 | pci_release_regions(chip->pci); |
| 581 | pci_disable_device(chip->pci); |
| 582 | } |
| 583 | |
Takashi Iwai | f007dc0 | 2008-02-22 18:35:22 +0100 | [diff] [blame] | 584 | int oxygen_pci_probe(struct pci_dev *pci, int index, char *id, |
Clemens Ladisch | bb71858 | 2009-02-19 08:37:13 +0100 | [diff] [blame] | 585 | struct module *owner, |
Clemens Ladisch | 30459d7 | 2009-02-19 08:42:44 +0100 | [diff] [blame] | 586 | const struct pci_device_id *ids, |
| 587 | int (*get_model)(struct oxygen *chip, |
| 588 | const struct pci_device_id *id |
| 589 | ) |
| 590 | ) |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 591 | { |
| 592 | struct snd_card *card; |
| 593 | struct oxygen *chip; |
Clemens Ladisch | 30459d7 | 2009-02-19 08:42:44 +0100 | [diff] [blame] | 594 | const struct pci_device_id *pci_id; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 595 | int err; |
| 596 | |
Takashi Iwai | 60c5772 | 2014-01-29 14:20:19 +0100 | [diff] [blame] | 597 | err = snd_card_new(&pci->dev, index, id, owner, |
| 598 | sizeof(*chip), &card); |
Takashi Iwai | e58de7b | 2008-12-28 16:44:30 +0100 | [diff] [blame] | 599 | if (err < 0) |
| 600 | return err; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 601 | |
| 602 | chip = card->private_data; |
| 603 | chip->card = card; |
| 604 | chip->pci = pci; |
| 605 | chip->irq = -1; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 606 | spin_lock_init(&chip->reg_lock); |
| 607 | mutex_init(&chip->mutex); |
| 608 | INIT_WORK(&chip->spdif_input_bits_work, |
| 609 | oxygen_spdif_input_bits_changed); |
Clemens Ladisch | 7c01415 | 2008-01-28 08:36:55 +0100 | [diff] [blame] | 610 | INIT_WORK(&chip->gpio_work, oxygen_gpio_changed); |
Clemens Ladisch | 1e821dd | 2008-01-28 08:34:21 +0100 | [diff] [blame] | 611 | init_waitqueue_head(&chip->ac97_waitqueue); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 612 | |
| 613 | err = pci_enable_device(pci); |
| 614 | if (err < 0) |
| 615 | goto err_card; |
| 616 | |
Clemens Ladisch | a69bb3c | 2009-02-19 08:38:55 +0100 | [diff] [blame] | 617 | err = pci_request_regions(pci, DRIVER); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 618 | if (err < 0) { |
Takashi Iwai | 03d3ac2 | 2014-02-25 18:00:26 +0100 | [diff] [blame] | 619 | dev_err(card->dev, "cannot reserve PCI resources\n"); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 620 | goto err_pci_enable; |
| 621 | } |
| 622 | |
| 623 | if (!(pci_resource_flags(pci, 0) & IORESOURCE_IO) || |
Clemens Ladisch | c136500 | 2008-05-13 09:19:53 +0200 | [diff] [blame] | 624 | pci_resource_len(pci, 0) < OXYGEN_IO_SIZE) { |
Takashi Iwai | 03d3ac2 | 2014-02-25 18:00:26 +0100 | [diff] [blame] | 625 | dev_err(card->dev, "invalid PCI I/O range\n"); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 626 | err = -ENXIO; |
| 627 | goto err_pci_regions; |
| 628 | } |
| 629 | chip->addr = pci_resource_start(pci, 0); |
| 630 | |
Clemens Ladisch | 30459d7 | 2009-02-19 08:42:44 +0100 | [diff] [blame] | 631 | pci_id = oxygen_search_pci_id(chip, ids); |
| 632 | if (!pci_id) { |
| 633 | err = -ENODEV; |
| 634 | goto err_pci_regions; |
| 635 | } |
Clemens Ladisch | 1275d6f | 2009-02-19 08:44:12 +0100 | [diff] [blame] | 636 | oxygen_restore_eeprom(chip, pci_id); |
Clemens Ladisch | 30459d7 | 2009-02-19 08:42:44 +0100 | [diff] [blame] | 637 | err = get_model(chip, pci_id); |
| 638 | if (err < 0) |
| 639 | goto err_pci_regions; |
| 640 | |
Clemens Ladisch | 6ed9115 | 2009-02-19 08:38:25 +0100 | [diff] [blame] | 641 | if (chip->model.model_data_size) { |
Clemens Ladisch | 82af308 | 2009-02-27 09:27:44 +0100 | [diff] [blame] | 642 | chip->model_data = kzalloc(chip->model.model_data_size, |
Clemens Ladisch | 6ed9115 | 2009-02-19 08:38:25 +0100 | [diff] [blame] | 643 | GFP_KERNEL); |
| 644 | if (!chip->model_data) { |
| 645 | err = -ENOMEM; |
| 646 | goto err_pci_regions; |
| 647 | } |
| 648 | } |
| 649 | |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 650 | pci_set_master(pci); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 651 | card->private_free = oxygen_card_free; |
| 652 | |
Clemens Ladisch | 7cb4ced | 2010-10-06 10:57:50 +0200 | [diff] [blame] | 653 | configure_pcie_bridge(pci); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 654 | oxygen_init(chip); |
Clemens Ladisch | 9bd6a73 | 2008-09-22 08:55:19 +0200 | [diff] [blame] | 655 | chip->model.init(chip); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 656 | |
| 657 | err = request_irq(pci->irq, oxygen_interrupt, IRQF_SHARED, |
Takashi Iwai | 934c2b6 | 2011-06-10 16:36:37 +0200 | [diff] [blame] | 658 | KBUILD_MODNAME, chip); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 659 | if (err < 0) { |
Takashi Iwai | 03d3ac2 | 2014-02-25 18:00:26 +0100 | [diff] [blame] | 660 | dev_err(card->dev, "cannot grab interrupt %d\n", pci->irq); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 661 | goto err_card; |
| 662 | } |
| 663 | chip->irq = pci->irq; |
Takashi Iwai | bf17d20 | 2019-12-10 07:34:28 +0100 | [diff] [blame] | 664 | card->sync_irq = chip->irq; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 665 | |
Clemens Ladisch | 9bd6a73 | 2008-09-22 08:55:19 +0200 | [diff] [blame] | 666 | strcpy(card->driver, chip->model.chip); |
| 667 | strcpy(card->shortname, chip->model.shortname); |
Clemens Ladisch | a1f80fc | 2011-01-10 16:36:23 +0100 | [diff] [blame] | 668 | sprintf(card->longname, "%s at %#lx, irq %i", |
| 669 | chip->model.longname, chip->addr, chip->irq); |
Clemens Ladisch | 9bd6a73 | 2008-09-22 08:55:19 +0200 | [diff] [blame] | 670 | strcpy(card->mixername, chip->model.chip); |
| 671 | snd_component_add(card, chip->model.chip); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 672 | |
| 673 | err = oxygen_pcm_init(chip); |
| 674 | if (err < 0) |
| 675 | goto err_card; |
| 676 | |
| 677 | err = oxygen_mixer_init(chip); |
| 678 | if (err < 0) |
| 679 | goto err_card; |
| 680 | |
Clemens Ladisch | dbbbd67 | 2008-09-22 09:03:42 +0200 | [diff] [blame] | 681 | if (chip->model.device_config & (MIDI_OUTPUT | MIDI_INPUT)) { |
Clemens Ladisch | dba8b46 | 2011-09-13 11:24:41 +0200 | [diff] [blame] | 682 | unsigned int info_flags = |
| 683 | MPU401_INFO_INTEGRATED | MPU401_INFO_IRQ_HOOK; |
Clemens Ladisch | dbbbd67 | 2008-09-22 09:03:42 +0200 | [diff] [blame] | 684 | if (chip->model.device_config & MIDI_OUTPUT) |
| 685 | info_flags |= MPU401_INFO_OUTPUT; |
| 686 | if (chip->model.device_config & MIDI_INPUT) |
| 687 | info_flags |= MPU401_INFO_INPUT; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 688 | err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI, |
| 689 | chip->addr + OXYGEN_MPU401, |
Clemens Ladisch | dba8b46 | 2011-09-13 11:24:41 +0200 | [diff] [blame] | 690 | info_flags, -1, &chip->midi); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 691 | if (err < 0) |
| 692 | goto err_card; |
| 693 | } |
| 694 | |
| 695 | oxygen_proc_init(chip); |
| 696 | |
| 697 | spin_lock_irq(&chip->reg_lock); |
Clemens Ladisch | d76596b | 2008-09-22 09:02:08 +0200 | [diff] [blame] | 698 | if (chip->model.device_config & CAPTURE_1_FROM_SPDIF) |
Clemens Ladisch | 1d98c7d | 2008-04-09 09:16:33 +0200 | [diff] [blame] | 699 | chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT; |
| 700 | if (chip->has_ac97_0 | chip->has_ac97_1) |
| 701 | chip->interrupt_mask |= OXYGEN_INT_AC97; |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 702 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask); |
| 703 | spin_unlock_irq(&chip->reg_lock); |
| 704 | |
| 705 | err = snd_card_register(card); |
| 706 | if (err < 0) |
| 707 | goto err_card; |
| 708 | |
| 709 | pci_set_drvdata(pci, card); |
| 710 | return 0; |
| 711 | |
| 712 | err_pci_regions: |
| 713 | pci_release_regions(pci); |
| 714 | err_pci_enable: |
| 715 | pci_disable_device(pci); |
| 716 | err_card: |
| 717 | snd_card_free(card); |
| 718 | return err; |
| 719 | } |
| 720 | EXPORT_SYMBOL(oxygen_pci_probe); |
| 721 | |
Takashi Iwai | f007dc0 | 2008-02-22 18:35:22 +0100 | [diff] [blame] | 722 | void oxygen_pci_remove(struct pci_dev *pci) |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 723 | { |
| 724 | snd_card_free(pci_get_drvdata(pci)); |
Clemens Ladisch | d0ce994 | 2007-12-23 19:50:57 +0100 | [diff] [blame] | 725 | } |
| 726 | EXPORT_SYMBOL(oxygen_pci_remove); |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 727 | |
Takashi Iwai | c7561cd | 2012-08-14 18:12:04 +0200 | [diff] [blame] | 728 | #ifdef CONFIG_PM_SLEEP |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 729 | static int oxygen_pci_suspend(struct device *dev) |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 730 | { |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 731 | struct snd_card *card = dev_get_drvdata(dev); |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 732 | struct oxygen *chip = card->private_data; |
Takashi Iwai | 17bc481 | 2019-01-11 18:02:51 +0100 | [diff] [blame] | 733 | unsigned int saved_interrupt_mask; |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 734 | |
| 735 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); |
| 736 | |
Clemens Ladisch | 9bd6a73 | 2008-09-22 08:55:19 +0200 | [diff] [blame] | 737 | if (chip->model.suspend) |
| 738 | chip->model.suspend(chip); |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 739 | |
| 740 | spin_lock_irq(&chip->reg_lock); |
| 741 | saved_interrupt_mask = chip->interrupt_mask; |
| 742 | chip->interrupt_mask = 0; |
| 743 | oxygen_write16(chip, OXYGEN_DMA_STATUS, 0); |
| 744 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0); |
| 745 | spin_unlock_irq(&chip->reg_lock); |
| 746 | |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 747 | flush_work(&chip->spdif_input_bits_work); |
| 748 | flush_work(&chip->gpio_work); |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 749 | chip->interrupt_mask = saved_interrupt_mask; |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 750 | return 0; |
| 751 | } |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 752 | |
| 753 | static const u32 registers_to_restore[OXYGEN_IO_SIZE / 32] = { |
| 754 | 0xffffffff, 0x00ff077f, 0x00011d08, 0x007f00ff, |
| 755 | 0x00300000, 0x00000fe4, 0x0ff7001f, 0x00000000 |
| 756 | }; |
| 757 | static const u32 ac97_registers_to_restore[2][0x40 / 32] = { |
| 758 | { 0x18284fa2, 0x03060000 }, |
| 759 | { 0x00007fa6, 0x00200000 } |
| 760 | }; |
| 761 | |
| 762 | static inline int is_bit_set(const u32 *bitmap, unsigned int bit) |
| 763 | { |
| 764 | return bitmap[bit / 32] & (1 << (bit & 31)); |
| 765 | } |
| 766 | |
| 767 | static void oxygen_restore_ac97(struct oxygen *chip, unsigned int codec) |
| 768 | { |
| 769 | unsigned int i; |
| 770 | |
| 771 | oxygen_write_ac97(chip, codec, AC97_RESET, 0); |
| 772 | msleep(1); |
| 773 | for (i = 1; i < 0x40; ++i) |
| 774 | if (is_bit_set(ac97_registers_to_restore[codec], i)) |
| 775 | oxygen_write_ac97(chip, codec, i * 2, |
| 776 | chip->saved_ac97_registers[codec][i]); |
| 777 | } |
| 778 | |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 779 | static int oxygen_pci_resume(struct device *dev) |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 780 | { |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 781 | struct snd_card *card = dev_get_drvdata(dev); |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 782 | struct oxygen *chip = card->private_data; |
| 783 | unsigned int i; |
| 784 | |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 785 | oxygen_write16(chip, OXYGEN_DMA_STATUS, 0); |
| 786 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0); |
| 787 | for (i = 0; i < OXYGEN_IO_SIZE; ++i) |
| 788 | if (is_bit_set(registers_to_restore, i)) |
| 789 | oxygen_write8(chip, i, chip->saved_registers._8[i]); |
| 790 | if (chip->has_ac97_0) |
| 791 | oxygen_restore_ac97(chip, 0); |
| 792 | if (chip->has_ac97_1) |
| 793 | oxygen_restore_ac97(chip, 1); |
| 794 | |
Clemens Ladisch | 9bd6a73 | 2008-09-22 08:55:19 +0200 | [diff] [blame] | 795 | if (chip->model.resume) |
| 796 | chip->model.resume(chip); |
Clemens Ladisch | 4a4bc53 | 2008-05-13 09:24:39 +0200 | [diff] [blame] | 797 | |
| 798 | oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask); |
| 799 | |
| 800 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); |
| 801 | return 0; |
| 802 | } |
Takashi Iwai | 68cb2b5 | 2012-07-02 15:20:37 +0200 | [diff] [blame] | 803 | |
| 804 | SIMPLE_DEV_PM_OPS(oxygen_pci_pm, oxygen_pci_suspend, oxygen_pci_resume); |
| 805 | EXPORT_SYMBOL(oxygen_pci_pm); |
Takashi Iwai | c7561cd | 2012-08-14 18:12:04 +0200 | [diff] [blame] | 806 | #endif /* CONFIG_PM_SLEEP */ |
Clemens Ladisch | 4c25b93 | 2010-09-07 13:37:10 +0200 | [diff] [blame] | 807 | |
| 808 | void oxygen_pci_shutdown(struct pci_dev *pci) |
| 809 | { |
| 810 | struct snd_card *card = pci_get_drvdata(pci); |
| 811 | struct oxygen *chip = card->private_data; |
| 812 | |
| 813 | oxygen_shutdown(chip); |
| 814 | chip->model.cleanup(chip); |
| 815 | } |
| 816 | EXPORT_SYMBOL(oxygen_pci_shutdown); |