18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * C-Media CMI8788 driver - main driver module
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/delay.h>
98c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
108c2ecf20Sopenharmony_ci#include <linux/mutex.h>
118c2ecf20Sopenharmony_ci#include <linux/pci.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <sound/ac97_codec.h>
158c2ecf20Sopenharmony_ci#include <sound/asoundef.h>
168c2ecf20Sopenharmony_ci#include <sound/core.h>
178c2ecf20Sopenharmony_ci#include <sound/info.h>
188c2ecf20Sopenharmony_ci#include <sound/mpu401.h>
198c2ecf20Sopenharmony_ci#include <sound/pcm.h>
208c2ecf20Sopenharmony_ci#include "oxygen.h"
218c2ecf20Sopenharmony_ci#include "cm9780.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciMODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
248c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("C-Media CMI8788 helper library");
258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define DRIVER "oxygen"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic inline int oxygen_uart_input_ready(struct oxygen *chip)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	return !(oxygen_read8(chip, OXYGEN_MPU401 + 1) & MPU401_RX_EMPTY);
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic void oxygen_read_uart(struct oxygen *chip)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	if (unlikely(!oxygen_uart_input_ready(chip))) {
378c2ecf20Sopenharmony_ci		/* no data, but read it anyway to clear the interrupt */
388c2ecf20Sopenharmony_ci		oxygen_read8(chip, OXYGEN_MPU401);
398c2ecf20Sopenharmony_ci		return;
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci	do {
428c2ecf20Sopenharmony_ci		u8 data = oxygen_read8(chip, OXYGEN_MPU401);
438c2ecf20Sopenharmony_ci		if (data == MPU401_ACK)
448c2ecf20Sopenharmony_ci			continue;
458c2ecf20Sopenharmony_ci		if (chip->uart_input_count >= ARRAY_SIZE(chip->uart_input))
468c2ecf20Sopenharmony_ci			chip->uart_input_count = 0;
478c2ecf20Sopenharmony_ci		chip->uart_input[chip->uart_input_count++] = data;
488c2ecf20Sopenharmony_ci	} while (oxygen_uart_input_ready(chip));
498c2ecf20Sopenharmony_ci	if (chip->model.uart_input)
508c2ecf20Sopenharmony_ci		chip->model.uart_input(chip);
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic irqreturn_t oxygen_interrupt(int dummy, void *dev_id)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	struct oxygen *chip = dev_id;
568c2ecf20Sopenharmony_ci	unsigned int status, clear, elapsed_streams, i;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	status = oxygen_read16(chip, OXYGEN_INTERRUPT_STATUS);
598c2ecf20Sopenharmony_ci	if (!status)
608c2ecf20Sopenharmony_ci		return IRQ_NONE;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	spin_lock(&chip->reg_lock);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	clear = status & (OXYGEN_CHANNEL_A |
658c2ecf20Sopenharmony_ci			  OXYGEN_CHANNEL_B |
668c2ecf20Sopenharmony_ci			  OXYGEN_CHANNEL_C |
678c2ecf20Sopenharmony_ci			  OXYGEN_CHANNEL_SPDIF |
688c2ecf20Sopenharmony_ci			  OXYGEN_CHANNEL_MULTICH |
698c2ecf20Sopenharmony_ci			  OXYGEN_CHANNEL_AC97 |
708c2ecf20Sopenharmony_ci			  OXYGEN_INT_SPDIF_IN_DETECT |
718c2ecf20Sopenharmony_ci			  OXYGEN_INT_GPIO |
728c2ecf20Sopenharmony_ci			  OXYGEN_INT_AC97);
738c2ecf20Sopenharmony_ci	if (clear) {
748c2ecf20Sopenharmony_ci		if (clear & OXYGEN_INT_SPDIF_IN_DETECT)
758c2ecf20Sopenharmony_ci			chip->interrupt_mask &= ~OXYGEN_INT_SPDIF_IN_DETECT;
768c2ecf20Sopenharmony_ci		oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
778c2ecf20Sopenharmony_ci			       chip->interrupt_mask & ~clear);
788c2ecf20Sopenharmony_ci		oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
798c2ecf20Sopenharmony_ci			       chip->interrupt_mask);
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	elapsed_streams = status & chip->pcm_running;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	spin_unlock(&chip->reg_lock);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	for (i = 0; i < PCM_COUNT; ++i)
878c2ecf20Sopenharmony_ci		if ((elapsed_streams & (1 << i)) && chip->streams[i])
888c2ecf20Sopenharmony_ci			snd_pcm_period_elapsed(chip->streams[i]);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	if (status & OXYGEN_INT_SPDIF_IN_DETECT) {
918c2ecf20Sopenharmony_ci		spin_lock(&chip->reg_lock);
928c2ecf20Sopenharmony_ci		i = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
938c2ecf20Sopenharmony_ci		if (i & (OXYGEN_SPDIF_SENSE_INT | OXYGEN_SPDIF_LOCK_INT |
948c2ecf20Sopenharmony_ci			 OXYGEN_SPDIF_RATE_INT)) {
958c2ecf20Sopenharmony_ci			/* write the interrupt bit(s) to clear */
968c2ecf20Sopenharmony_ci			oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, i);
978c2ecf20Sopenharmony_ci			schedule_work(&chip->spdif_input_bits_work);
988c2ecf20Sopenharmony_ci		}
998c2ecf20Sopenharmony_ci		spin_unlock(&chip->reg_lock);
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	if (status & OXYGEN_INT_GPIO)
1038c2ecf20Sopenharmony_ci		schedule_work(&chip->gpio_work);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (status & OXYGEN_INT_MIDI) {
1068c2ecf20Sopenharmony_ci		if (chip->midi)
1078c2ecf20Sopenharmony_ci			snd_mpu401_uart_interrupt(0, chip->midi->private_data);
1088c2ecf20Sopenharmony_ci		else
1098c2ecf20Sopenharmony_ci			oxygen_read_uart(chip);
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (status & OXYGEN_INT_AC97)
1138c2ecf20Sopenharmony_ci		wake_up(&chip->ac97_waitqueue);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic void oxygen_spdif_input_bits_changed(struct work_struct *work)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	struct oxygen *chip = container_of(work, struct oxygen,
1218c2ecf20Sopenharmony_ci					   spdif_input_bits_work);
1228c2ecf20Sopenharmony_ci	u32 reg;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/*
1258c2ecf20Sopenharmony_ci	 * This function gets called when there is new activity on the SPDIF
1268c2ecf20Sopenharmony_ci	 * input, or when we lose lock on the input signal, or when the rate
1278c2ecf20Sopenharmony_ci	 * changes.
1288c2ecf20Sopenharmony_ci	 */
1298c2ecf20Sopenharmony_ci	msleep(1);
1308c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->reg_lock);
1318c2ecf20Sopenharmony_ci	reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
1328c2ecf20Sopenharmony_ci	if ((reg & (OXYGEN_SPDIF_SENSE_STATUS |
1338c2ecf20Sopenharmony_ci		    OXYGEN_SPDIF_LOCK_STATUS))
1348c2ecf20Sopenharmony_ci	    == OXYGEN_SPDIF_SENSE_STATUS) {
1358c2ecf20Sopenharmony_ci		/*
1368c2ecf20Sopenharmony_ci		 * If we detect activity on the SPDIF input but cannot lock to
1378c2ecf20Sopenharmony_ci		 * a signal, the clock bit is likely to be wrong.
1388c2ecf20Sopenharmony_ci		 */
1398c2ecf20Sopenharmony_ci		reg ^= OXYGEN_SPDIF_IN_CLOCK_MASK;
1408c2ecf20Sopenharmony_ci		oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg);
1418c2ecf20Sopenharmony_ci		spin_unlock_irq(&chip->reg_lock);
1428c2ecf20Sopenharmony_ci		msleep(1);
1438c2ecf20Sopenharmony_ci		spin_lock_irq(&chip->reg_lock);
1448c2ecf20Sopenharmony_ci		reg = oxygen_read32(chip, OXYGEN_SPDIF_CONTROL);
1458c2ecf20Sopenharmony_ci		if ((reg & (OXYGEN_SPDIF_SENSE_STATUS |
1468c2ecf20Sopenharmony_ci			    OXYGEN_SPDIF_LOCK_STATUS))
1478c2ecf20Sopenharmony_ci		    == OXYGEN_SPDIF_SENSE_STATUS) {
1488c2ecf20Sopenharmony_ci			/* nothing detected with either clock; give up */
1498c2ecf20Sopenharmony_ci			if ((reg & OXYGEN_SPDIF_IN_CLOCK_MASK)
1508c2ecf20Sopenharmony_ci			    == OXYGEN_SPDIF_IN_CLOCK_192) {
1518c2ecf20Sopenharmony_ci				/*
1528c2ecf20Sopenharmony_ci				 * Reset clock to <= 96 kHz because this is
1538c2ecf20Sopenharmony_ci				 * more likely to be received next time.
1548c2ecf20Sopenharmony_ci				 */
1558c2ecf20Sopenharmony_ci				reg &= ~OXYGEN_SPDIF_IN_CLOCK_MASK;
1568c2ecf20Sopenharmony_ci				reg |= OXYGEN_SPDIF_IN_CLOCK_96;
1578c2ecf20Sopenharmony_ci				oxygen_write32(chip, OXYGEN_SPDIF_CONTROL, reg);
1588c2ecf20Sopenharmony_ci			}
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->reg_lock);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	if (chip->controls[CONTROL_SPDIF_INPUT_BITS]) {
1648c2ecf20Sopenharmony_ci		spin_lock_irq(&chip->reg_lock);
1658c2ecf20Sopenharmony_ci		chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT;
1668c2ecf20Sopenharmony_ci		oxygen_write16(chip, OXYGEN_INTERRUPT_MASK,
1678c2ecf20Sopenharmony_ci			       chip->interrupt_mask);
1688c2ecf20Sopenharmony_ci		spin_unlock_irq(&chip->reg_lock);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci		/*
1718c2ecf20Sopenharmony_ci		 * We don't actually know that any channel status bits have
1728c2ecf20Sopenharmony_ci		 * changed, but let's send a notification just to be sure.
1738c2ecf20Sopenharmony_ci		 */
1748c2ecf20Sopenharmony_ci		snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1758c2ecf20Sopenharmony_ci			       &chip->controls[CONTROL_SPDIF_INPUT_BITS]->id);
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic void oxygen_gpio_changed(struct work_struct *work)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	struct oxygen *chip = container_of(work, struct oxygen, gpio_work);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (chip->model.gpio_changed)
1848c2ecf20Sopenharmony_ci		chip->model.gpio_changed(chip);
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic void oxygen_proc_read(struct snd_info_entry *entry,
1888c2ecf20Sopenharmony_ci			     struct snd_info_buffer *buffer)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	struct oxygen *chip = entry->private_data;
1918c2ecf20Sopenharmony_ci	int i, j;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	switch (oxygen_read8(chip, OXYGEN_REVISION) & OXYGEN_PACKAGE_ID_MASK) {
1948c2ecf20Sopenharmony_ci	case OXYGEN_PACKAGE_ID_8786: i = '6'; break;
1958c2ecf20Sopenharmony_ci	case OXYGEN_PACKAGE_ID_8787: i = '7'; break;
1968c2ecf20Sopenharmony_ci	case OXYGEN_PACKAGE_ID_8788: i = '8'; break;
1978c2ecf20Sopenharmony_ci	default:                     i = '?'; break;
1988c2ecf20Sopenharmony_ci	}
1998c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "CMI878%c:\n", i);
2008c2ecf20Sopenharmony_ci	for (i = 0; i < OXYGEN_IO_SIZE; i += 0x10) {
2018c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "%02x:", i);
2028c2ecf20Sopenharmony_ci		for (j = 0; j < 0x10; ++j)
2038c2ecf20Sopenharmony_ci			snd_iprintf(buffer, " %02x", oxygen_read8(chip, i + j));
2048c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "\n");
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&chip->mutex) < 0)
2078c2ecf20Sopenharmony_ci		return;
2088c2ecf20Sopenharmony_ci	if (chip->has_ac97_0) {
2098c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "\nAC97:\n");
2108c2ecf20Sopenharmony_ci		for (i = 0; i < 0x80; i += 0x10) {
2118c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "%02x:", i);
2128c2ecf20Sopenharmony_ci			for (j = 0; j < 0x10; j += 2)
2138c2ecf20Sopenharmony_ci				snd_iprintf(buffer, " %04x",
2148c2ecf20Sopenharmony_ci					    oxygen_read_ac97(chip, 0, i + j));
2158c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "\n");
2168c2ecf20Sopenharmony_ci		}
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci	if (chip->has_ac97_1) {
2198c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "\nAC97 2:\n");
2208c2ecf20Sopenharmony_ci		for (i = 0; i < 0x80; i += 0x10) {
2218c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "%02x:", i);
2228c2ecf20Sopenharmony_ci			for (j = 0; j < 0x10; j += 2)
2238c2ecf20Sopenharmony_ci				snd_iprintf(buffer, " %04x",
2248c2ecf20Sopenharmony_ci					    oxygen_read_ac97(chip, 1, i + j));
2258c2ecf20Sopenharmony_ci			snd_iprintf(buffer, "\n");
2268c2ecf20Sopenharmony_ci		}
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci	mutex_unlock(&chip->mutex);
2298c2ecf20Sopenharmony_ci	if (chip->model.dump_registers)
2308c2ecf20Sopenharmony_ci		chip->model.dump_registers(chip, buffer);
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic void oxygen_proc_init(struct oxygen *chip)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	snd_card_ro_proc_new(chip->card, "oxygen", chip, oxygen_proc_read);
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic const struct pci_device_id *
2398c2ecf20Sopenharmony_cioxygen_search_pci_id(struct oxygen *chip, const struct pci_device_id ids[])
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci	u16 subdevice;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/*
2448c2ecf20Sopenharmony_ci	 * Make sure the EEPROM pins are available, i.e., not used for SPI.
2458c2ecf20Sopenharmony_ci	 * (This function is called before we initialize or use SPI.)
2468c2ecf20Sopenharmony_ci	 */
2478c2ecf20Sopenharmony_ci	oxygen_clear_bits8(chip, OXYGEN_FUNCTION,
2488c2ecf20Sopenharmony_ci			   OXYGEN_FUNCTION_ENABLE_SPI_4_5);
2498c2ecf20Sopenharmony_ci	/*
2508c2ecf20Sopenharmony_ci	 * Read the subsystem device ID directly from the EEPROM, because the
2518c2ecf20Sopenharmony_ci	 * chip didn't if the first EEPROM word was overwritten.
2528c2ecf20Sopenharmony_ci	 */
2538c2ecf20Sopenharmony_ci	subdevice = oxygen_read_eeprom(chip, 2);
2548c2ecf20Sopenharmony_ci	/* use default ID if EEPROM is missing */
2558c2ecf20Sopenharmony_ci	if (subdevice == 0xffff && oxygen_read_eeprom(chip, 1) == 0xffff)
2568c2ecf20Sopenharmony_ci		subdevice = 0x8788;
2578c2ecf20Sopenharmony_ci	/*
2588c2ecf20Sopenharmony_ci	 * We use only the subsystem device ID for searching because it is
2598c2ecf20Sopenharmony_ci	 * unique even without the subsystem vendor ID, which may have been
2608c2ecf20Sopenharmony_ci	 * overwritten in the EEPROM.
2618c2ecf20Sopenharmony_ci	 */
2628c2ecf20Sopenharmony_ci	for (; ids->vendor; ++ids)
2638c2ecf20Sopenharmony_ci		if (ids->subdevice == subdevice &&
2648c2ecf20Sopenharmony_ci		    ids->driver_data != BROKEN_EEPROM_DRIVER_DATA)
2658c2ecf20Sopenharmony_ci			return ids;
2668c2ecf20Sopenharmony_ci	return NULL;
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic void oxygen_restore_eeprom(struct oxygen *chip,
2708c2ecf20Sopenharmony_ci				  const struct pci_device_id *id)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	u16 eeprom_id;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	eeprom_id = oxygen_read_eeprom(chip, 0);
2758c2ecf20Sopenharmony_ci	if (eeprom_id != OXYGEN_EEPROM_ID &&
2768c2ecf20Sopenharmony_ci	    (eeprom_id != 0xffff || id->subdevice != 0x8788)) {
2778c2ecf20Sopenharmony_ci		/*
2788c2ecf20Sopenharmony_ci		 * This function gets called only when a known card model has
2798c2ecf20Sopenharmony_ci		 * been detected, i.e., we know there is a valid subsystem
2808c2ecf20Sopenharmony_ci		 * product ID at index 2 in the EEPROM.  Therefore, we have
2818c2ecf20Sopenharmony_ci		 * been able to deduce the correct subsystem vendor ID, and
2828c2ecf20Sopenharmony_ci		 * this is enough information to restore the original EEPROM
2838c2ecf20Sopenharmony_ci		 * contents.
2848c2ecf20Sopenharmony_ci		 */
2858c2ecf20Sopenharmony_ci		oxygen_write_eeprom(chip, 1, id->subvendor);
2868c2ecf20Sopenharmony_ci		oxygen_write_eeprom(chip, 0, OXYGEN_EEPROM_ID);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		oxygen_set_bits8(chip, OXYGEN_MISC,
2898c2ecf20Sopenharmony_ci				 OXYGEN_MISC_WRITE_PCI_SUBID);
2908c2ecf20Sopenharmony_ci		pci_write_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID,
2918c2ecf20Sopenharmony_ci				      id->subvendor);
2928c2ecf20Sopenharmony_ci		pci_write_config_word(chip->pci, PCI_SUBSYSTEM_ID,
2938c2ecf20Sopenharmony_ci				      id->subdevice);
2948c2ecf20Sopenharmony_ci		oxygen_clear_bits8(chip, OXYGEN_MISC,
2958c2ecf20Sopenharmony_ci				   OXYGEN_MISC_WRITE_PCI_SUBID);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci		dev_info(chip->card->dev, "EEPROM ID restored\n");
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic void configure_pcie_bridge(struct pci_dev *pci)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	enum { PEX811X, PI7C9X110, XIO2001 };
3048c2ecf20Sopenharmony_ci	static const struct pci_device_id bridge_ids[] = {
3058c2ecf20Sopenharmony_ci		{ PCI_VDEVICE(PLX, 0x8111), .driver_data = PEX811X },
3068c2ecf20Sopenharmony_ci		{ PCI_VDEVICE(PLX, 0x8112), .driver_data = PEX811X },
3078c2ecf20Sopenharmony_ci		{ PCI_DEVICE(0x12d8, 0xe110), .driver_data = PI7C9X110 },
3088c2ecf20Sopenharmony_ci		{ PCI_VDEVICE(TI, 0x8240), .driver_data = XIO2001 },
3098c2ecf20Sopenharmony_ci		{ }
3108c2ecf20Sopenharmony_ci	};
3118c2ecf20Sopenharmony_ci	struct pci_dev *bridge;
3128c2ecf20Sopenharmony_ci	const struct pci_device_id *id;
3138c2ecf20Sopenharmony_ci	u32 tmp;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	if (!pci->bus || !pci->bus->self)
3168c2ecf20Sopenharmony_ci		return;
3178c2ecf20Sopenharmony_ci	bridge = pci->bus->self;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	id = pci_match_id(bridge_ids, bridge);
3208c2ecf20Sopenharmony_ci	if (!id)
3218c2ecf20Sopenharmony_ci		return;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	switch (id->driver_data) {
3248c2ecf20Sopenharmony_ci	case PEX811X:	/* PLX PEX8111/PEX8112 PCIe/PCI bridge */
3258c2ecf20Sopenharmony_ci		pci_read_config_dword(bridge, 0x48, &tmp);
3268c2ecf20Sopenharmony_ci		tmp |= 1;	/* enable blind prefetching */
3278c2ecf20Sopenharmony_ci		tmp |= 1 << 11;	/* enable beacon generation */
3288c2ecf20Sopenharmony_ci		pci_write_config_dword(bridge, 0x48, tmp);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci		pci_write_config_dword(bridge, 0x84, 0x0c);
3318c2ecf20Sopenharmony_ci		pci_read_config_dword(bridge, 0x88, &tmp);
3328c2ecf20Sopenharmony_ci		tmp &= ~(7 << 27);
3338c2ecf20Sopenharmony_ci		tmp |= 2 << 27;	/* set prefetch size to 128 bytes */
3348c2ecf20Sopenharmony_ci		pci_write_config_dword(bridge, 0x88, tmp);
3358c2ecf20Sopenharmony_ci		break;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	case PI7C9X110:	/* Pericom PI7C9X110 PCIe/PCI bridge */
3388c2ecf20Sopenharmony_ci		pci_read_config_dword(bridge, 0x40, &tmp);
3398c2ecf20Sopenharmony_ci		tmp |= 1;	/* park the PCI arbiter to the sound chip */
3408c2ecf20Sopenharmony_ci		pci_write_config_dword(bridge, 0x40, tmp);
3418c2ecf20Sopenharmony_ci		break;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	case XIO2001: /* Texas Instruments XIO2001 PCIe/PCI bridge */
3448c2ecf20Sopenharmony_ci		pci_read_config_dword(bridge, 0xe8, &tmp);
3458c2ecf20Sopenharmony_ci		tmp &= ~0xf;	/* request length limit: 64 bytes */
3468c2ecf20Sopenharmony_ci		tmp &= ~(0xf << 8);
3478c2ecf20Sopenharmony_ci		tmp |= 1 << 8;	/* request count limit: one buffer */
3488c2ecf20Sopenharmony_ci		pci_write_config_dword(bridge, 0xe8, tmp);
3498c2ecf20Sopenharmony_ci		break;
3508c2ecf20Sopenharmony_ci	}
3518c2ecf20Sopenharmony_ci}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_cistatic void oxygen_init(struct oxygen *chip)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	unsigned int i;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	chip->dac_routing = 1;
3588c2ecf20Sopenharmony_ci	for (i = 0; i < 8; ++i)
3598c2ecf20Sopenharmony_ci		chip->dac_volume[i] = chip->model.dac_volume_min;
3608c2ecf20Sopenharmony_ci	chip->dac_mute = 1;
3618c2ecf20Sopenharmony_ci	chip->spdif_playback_enable = 0;
3628c2ecf20Sopenharmony_ci	chip->spdif_bits = OXYGEN_SPDIF_C | OXYGEN_SPDIF_ORIGINAL |
3638c2ecf20Sopenharmony_ci		(IEC958_AES1_CON_PCM_CODER << OXYGEN_SPDIF_CATEGORY_SHIFT);
3648c2ecf20Sopenharmony_ci	chip->spdif_pcm_bits = chip->spdif_bits;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	if (!(oxygen_read8(chip, OXYGEN_REVISION) & OXYGEN_REVISION_2))
3678c2ecf20Sopenharmony_ci		oxygen_set_bits8(chip, OXYGEN_MISC,
3688c2ecf20Sopenharmony_ci				 OXYGEN_MISC_PCI_MEM_W_1_CLOCK);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	i = oxygen_read16(chip, OXYGEN_AC97_CONTROL);
3718c2ecf20Sopenharmony_ci	chip->has_ac97_0 = (i & OXYGEN_AC97_CODEC_0) != 0;
3728c2ecf20Sopenharmony_ci	chip->has_ac97_1 = (i & OXYGEN_AC97_CODEC_1) != 0;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	oxygen_write8_masked(chip, OXYGEN_FUNCTION,
3758c2ecf20Sopenharmony_ci			     OXYGEN_FUNCTION_RESET_CODEC |
3768c2ecf20Sopenharmony_ci			     chip->model.function_flags,
3778c2ecf20Sopenharmony_ci			     OXYGEN_FUNCTION_RESET_CODEC |
3788c2ecf20Sopenharmony_ci			     OXYGEN_FUNCTION_2WIRE_SPI_MASK |
3798c2ecf20Sopenharmony_ci			     OXYGEN_FUNCTION_ENABLE_SPI_4_5);
3808c2ecf20Sopenharmony_ci	oxygen_write8(chip, OXYGEN_DMA_STATUS, 0);
3818c2ecf20Sopenharmony_ci	oxygen_write8(chip, OXYGEN_DMA_PAUSE, 0);
3828c2ecf20Sopenharmony_ci	oxygen_write8(chip, OXYGEN_PLAY_CHANNELS,
3838c2ecf20Sopenharmony_ci		      OXYGEN_PLAY_CHANNELS_2 |
3848c2ecf20Sopenharmony_ci		      OXYGEN_DMA_A_BURST_8 |
3858c2ecf20Sopenharmony_ci		      OXYGEN_DMA_MULTICH_BURST_8);
3868c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
3878c2ecf20Sopenharmony_ci	oxygen_write8_masked(chip, OXYGEN_MISC,
3888c2ecf20Sopenharmony_ci			     chip->model.misc_flags,
3898c2ecf20Sopenharmony_ci			     OXYGEN_MISC_WRITE_PCI_SUBID |
3908c2ecf20Sopenharmony_ci			     OXYGEN_MISC_REC_C_FROM_SPDIF |
3918c2ecf20Sopenharmony_ci			     OXYGEN_MISC_REC_B_FROM_AC97 |
3928c2ecf20Sopenharmony_ci			     OXYGEN_MISC_REC_A_FROM_MULTICH |
3938c2ecf20Sopenharmony_ci			     OXYGEN_MISC_MIDI);
3948c2ecf20Sopenharmony_ci	oxygen_write8(chip, OXYGEN_REC_FORMAT,
3958c2ecf20Sopenharmony_ci		      (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_A_SHIFT) |
3968c2ecf20Sopenharmony_ci		      (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_B_SHIFT) |
3978c2ecf20Sopenharmony_ci		      (OXYGEN_FORMAT_16 << OXYGEN_REC_FORMAT_C_SHIFT));
3988c2ecf20Sopenharmony_ci	oxygen_write8(chip, OXYGEN_PLAY_FORMAT,
3998c2ecf20Sopenharmony_ci		      (OXYGEN_FORMAT_16 << OXYGEN_SPDIF_FORMAT_SHIFT) |
4008c2ecf20Sopenharmony_ci		      (OXYGEN_FORMAT_16 << OXYGEN_MULTICH_FORMAT_SHIFT));
4018c2ecf20Sopenharmony_ci	oxygen_write8(chip, OXYGEN_REC_CHANNELS, OXYGEN_REC_CHANNELS_2_2_2);
4028c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_I2S_MULTICH_FORMAT,
4038c2ecf20Sopenharmony_ci		       OXYGEN_RATE_48000 |
4048c2ecf20Sopenharmony_ci		       chip->model.dac_i2s_format |
4058c2ecf20Sopenharmony_ci		       OXYGEN_I2S_MCLK(chip->model.dac_mclks) |
4068c2ecf20Sopenharmony_ci		       OXYGEN_I2S_BITS_16 |
4078c2ecf20Sopenharmony_ci		       OXYGEN_I2S_MASTER |
4088c2ecf20Sopenharmony_ci		       OXYGEN_I2S_BCLK_64);
4098c2ecf20Sopenharmony_ci	if (chip->model.device_config & CAPTURE_0_FROM_I2S_1)
4108c2ecf20Sopenharmony_ci		oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
4118c2ecf20Sopenharmony_ci			       OXYGEN_RATE_48000 |
4128c2ecf20Sopenharmony_ci			       chip->model.adc_i2s_format |
4138c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MCLK(chip->model.adc_mclks) |
4148c2ecf20Sopenharmony_ci			       OXYGEN_I2S_BITS_16 |
4158c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MASTER |
4168c2ecf20Sopenharmony_ci			       OXYGEN_I2S_BCLK_64);
4178c2ecf20Sopenharmony_ci	else
4188c2ecf20Sopenharmony_ci		oxygen_write16(chip, OXYGEN_I2S_A_FORMAT,
4198c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MASTER |
4208c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MUTE_MCLK);
4218c2ecf20Sopenharmony_ci	if (chip->model.device_config & (CAPTURE_0_FROM_I2S_2 |
4228c2ecf20Sopenharmony_ci					 CAPTURE_2_FROM_I2S_2))
4238c2ecf20Sopenharmony_ci		oxygen_write16(chip, OXYGEN_I2S_B_FORMAT,
4248c2ecf20Sopenharmony_ci			       OXYGEN_RATE_48000 |
4258c2ecf20Sopenharmony_ci			       chip->model.adc_i2s_format |
4268c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MCLK(chip->model.adc_mclks) |
4278c2ecf20Sopenharmony_ci			       OXYGEN_I2S_BITS_16 |
4288c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MASTER |
4298c2ecf20Sopenharmony_ci			       OXYGEN_I2S_BCLK_64);
4308c2ecf20Sopenharmony_ci	else
4318c2ecf20Sopenharmony_ci		oxygen_write16(chip, OXYGEN_I2S_B_FORMAT,
4328c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MASTER |
4338c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MUTE_MCLK);
4348c2ecf20Sopenharmony_ci	if (chip->model.device_config & CAPTURE_3_FROM_I2S_3)
4358c2ecf20Sopenharmony_ci		oxygen_write16(chip, OXYGEN_I2S_C_FORMAT,
4368c2ecf20Sopenharmony_ci			       OXYGEN_RATE_48000 |
4378c2ecf20Sopenharmony_ci			       chip->model.adc_i2s_format |
4388c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MCLK(chip->model.adc_mclks) |
4398c2ecf20Sopenharmony_ci			       OXYGEN_I2S_BITS_16 |
4408c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MASTER |
4418c2ecf20Sopenharmony_ci			       OXYGEN_I2S_BCLK_64);
4428c2ecf20Sopenharmony_ci	else
4438c2ecf20Sopenharmony_ci		oxygen_write16(chip, OXYGEN_I2S_C_FORMAT,
4448c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MASTER |
4458c2ecf20Sopenharmony_ci			       OXYGEN_I2S_MUTE_MCLK);
4468c2ecf20Sopenharmony_ci	oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
4478c2ecf20Sopenharmony_ci			    OXYGEN_SPDIF_OUT_ENABLE |
4488c2ecf20Sopenharmony_ci			    OXYGEN_SPDIF_LOOPBACK);
4498c2ecf20Sopenharmony_ci	if (chip->model.device_config & CAPTURE_1_FROM_SPDIF)
4508c2ecf20Sopenharmony_ci		oxygen_write32_masked(chip, OXYGEN_SPDIF_CONTROL,
4518c2ecf20Sopenharmony_ci				      OXYGEN_SPDIF_SENSE_MASK |
4528c2ecf20Sopenharmony_ci				      OXYGEN_SPDIF_LOCK_MASK |
4538c2ecf20Sopenharmony_ci				      OXYGEN_SPDIF_RATE_MASK |
4548c2ecf20Sopenharmony_ci				      OXYGEN_SPDIF_LOCK_PAR |
4558c2ecf20Sopenharmony_ci				      OXYGEN_SPDIF_IN_CLOCK_96,
4568c2ecf20Sopenharmony_ci				      OXYGEN_SPDIF_SENSE_MASK |
4578c2ecf20Sopenharmony_ci				      OXYGEN_SPDIF_LOCK_MASK |
4588c2ecf20Sopenharmony_ci				      OXYGEN_SPDIF_RATE_MASK |
4598c2ecf20Sopenharmony_ci				      OXYGEN_SPDIF_SENSE_PAR |
4608c2ecf20Sopenharmony_ci				      OXYGEN_SPDIF_LOCK_PAR |
4618c2ecf20Sopenharmony_ci				      OXYGEN_SPDIF_IN_CLOCK_MASK);
4628c2ecf20Sopenharmony_ci	else
4638c2ecf20Sopenharmony_ci		oxygen_clear_bits32(chip, OXYGEN_SPDIF_CONTROL,
4648c2ecf20Sopenharmony_ci				    OXYGEN_SPDIF_SENSE_MASK |
4658c2ecf20Sopenharmony_ci				    OXYGEN_SPDIF_LOCK_MASK |
4668c2ecf20Sopenharmony_ci				    OXYGEN_SPDIF_RATE_MASK);
4678c2ecf20Sopenharmony_ci	oxygen_write32(chip, OXYGEN_SPDIF_OUTPUT_BITS, chip->spdif_bits);
4688c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_2WIRE_BUS_STATUS,
4698c2ecf20Sopenharmony_ci		       OXYGEN_2WIRE_LENGTH_8 |
4708c2ecf20Sopenharmony_ci		       OXYGEN_2WIRE_INTERRUPT_MASK |
4718c2ecf20Sopenharmony_ci		       OXYGEN_2WIRE_SPEED_STANDARD);
4728c2ecf20Sopenharmony_ci	oxygen_clear_bits8(chip, OXYGEN_MPU401_CONTROL, OXYGEN_MPU401_LOOPBACK);
4738c2ecf20Sopenharmony_ci	oxygen_write8(chip, OXYGEN_GPI_INTERRUPT_MASK, 0);
4748c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_GPIO_INTERRUPT_MASK, 0);
4758c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_PLAY_ROUTING,
4768c2ecf20Sopenharmony_ci		       OXYGEN_PLAY_MULTICH_I2S_DAC |
4778c2ecf20Sopenharmony_ci		       OXYGEN_PLAY_SPDIF_SPDIF |
4788c2ecf20Sopenharmony_ci		       (0 << OXYGEN_PLAY_DAC0_SOURCE_SHIFT) |
4798c2ecf20Sopenharmony_ci		       (1 << OXYGEN_PLAY_DAC1_SOURCE_SHIFT) |
4808c2ecf20Sopenharmony_ci		       (2 << OXYGEN_PLAY_DAC2_SOURCE_SHIFT) |
4818c2ecf20Sopenharmony_ci		       (3 << OXYGEN_PLAY_DAC3_SOURCE_SHIFT));
4828c2ecf20Sopenharmony_ci	oxygen_write8(chip, OXYGEN_REC_ROUTING,
4838c2ecf20Sopenharmony_ci		      OXYGEN_REC_A_ROUTE_I2S_ADC_1 |
4848c2ecf20Sopenharmony_ci		      OXYGEN_REC_B_ROUTE_I2S_ADC_2 |
4858c2ecf20Sopenharmony_ci		      OXYGEN_REC_C_ROUTE_SPDIF);
4868c2ecf20Sopenharmony_ci	oxygen_write8(chip, OXYGEN_ADC_MONITOR, 0);
4878c2ecf20Sopenharmony_ci	oxygen_write8(chip, OXYGEN_A_MONITOR_ROUTING,
4888c2ecf20Sopenharmony_ci		      (0 << OXYGEN_A_MONITOR_ROUTE_0_SHIFT) |
4898c2ecf20Sopenharmony_ci		      (1 << OXYGEN_A_MONITOR_ROUTE_1_SHIFT) |
4908c2ecf20Sopenharmony_ci		      (2 << OXYGEN_A_MONITOR_ROUTE_2_SHIFT) |
4918c2ecf20Sopenharmony_ci		      (3 << OXYGEN_A_MONITOR_ROUTE_3_SHIFT));
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	if (chip->has_ac97_0 | chip->has_ac97_1)
4948c2ecf20Sopenharmony_ci		oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK,
4958c2ecf20Sopenharmony_ci			      OXYGEN_AC97_INT_READ_DONE |
4968c2ecf20Sopenharmony_ci			      OXYGEN_AC97_INT_WRITE_DONE);
4978c2ecf20Sopenharmony_ci	else
4988c2ecf20Sopenharmony_ci		oxygen_write8(chip, OXYGEN_AC97_INTERRUPT_MASK, 0);
4998c2ecf20Sopenharmony_ci	oxygen_write32(chip, OXYGEN_AC97_OUT_CONFIG, 0);
5008c2ecf20Sopenharmony_ci	oxygen_write32(chip, OXYGEN_AC97_IN_CONFIG, 0);
5018c2ecf20Sopenharmony_ci	if (!(chip->has_ac97_0 | chip->has_ac97_1))
5028c2ecf20Sopenharmony_ci		oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
5038c2ecf20Sopenharmony_ci				  OXYGEN_AC97_CLOCK_DISABLE);
5048c2ecf20Sopenharmony_ci	if (!chip->has_ac97_0) {
5058c2ecf20Sopenharmony_ci		oxygen_set_bits16(chip, OXYGEN_AC97_CONTROL,
5068c2ecf20Sopenharmony_ci				  OXYGEN_AC97_NO_CODEC_0);
5078c2ecf20Sopenharmony_ci	} else {
5088c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 0, AC97_RESET, 0);
5098c2ecf20Sopenharmony_ci		msleep(1);
5108c2ecf20Sopenharmony_ci		oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_SETUP,
5118c2ecf20Sopenharmony_ci				     CM9780_GPIO0IO | CM9780_GPIO1IO);
5128c2ecf20Sopenharmony_ci		oxygen_ac97_set_bits(chip, 0, CM9780_MIXER,
5138c2ecf20Sopenharmony_ci				     CM9780_BSTSEL | CM9780_STRO_MIC |
5148c2ecf20Sopenharmony_ci				     CM9780_MIX2FR | CM9780_PCBSW);
5158c2ecf20Sopenharmony_ci		oxygen_ac97_set_bits(chip, 0, CM9780_JACK,
5168c2ecf20Sopenharmony_ci				     CM9780_RSOE | CM9780_CBOE |
5178c2ecf20Sopenharmony_ci				     CM9780_SSOE | CM9780_FROE |
5188c2ecf20Sopenharmony_ci				     CM9780_MIC2MIC | CM9780_LI2LI);
5198c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 0, AC97_MASTER, 0x0000);
5208c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 0, AC97_PC_BEEP, 0x8000);
5218c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 0, AC97_MIC, 0x8808);
5228c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 0, AC97_LINE, 0x0808);
5238c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 0, AC97_CD, 0x8808);
5248c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 0, AC97_VIDEO, 0x8808);
5258c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 0, AC97_AUX, 0x8808);
5268c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 0, AC97_REC_GAIN, 0x8000);
5278c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 0, AC97_CENTER_LFE_MASTER, 0x8080);
5288c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 0, AC97_SURROUND_MASTER, 0x8080);
5298c2ecf20Sopenharmony_ci		oxygen_ac97_clear_bits(chip, 0, CM9780_GPIO_STATUS,
5308c2ecf20Sopenharmony_ci				       CM9780_GPO0);
5318c2ecf20Sopenharmony_ci		/* power down unused ADCs and DACs */
5328c2ecf20Sopenharmony_ci		oxygen_ac97_set_bits(chip, 0, AC97_POWERDOWN,
5338c2ecf20Sopenharmony_ci				     AC97_PD_PR0 | AC97_PD_PR1);
5348c2ecf20Sopenharmony_ci		oxygen_ac97_set_bits(chip, 0, AC97_EXTENDED_STATUS,
5358c2ecf20Sopenharmony_ci				     AC97_EA_PRI | AC97_EA_PRJ | AC97_EA_PRK);
5368c2ecf20Sopenharmony_ci	}
5378c2ecf20Sopenharmony_ci	if (chip->has_ac97_1) {
5388c2ecf20Sopenharmony_ci		oxygen_set_bits32(chip, OXYGEN_AC97_OUT_CONFIG,
5398c2ecf20Sopenharmony_ci				  OXYGEN_AC97_CODEC1_SLOT3 |
5408c2ecf20Sopenharmony_ci				  OXYGEN_AC97_CODEC1_SLOT4);
5418c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_RESET, 0);
5428c2ecf20Sopenharmony_ci		msleep(1);
5438c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_MASTER, 0x0000);
5448c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_HEADPHONE, 0x8000);
5458c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_PC_BEEP, 0x8000);
5468c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_MIC, 0x8808);
5478c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_LINE, 0x8808);
5488c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_CD, 0x8808);
5498c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_VIDEO, 0x8808);
5508c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_AUX, 0x8808);
5518c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_PCM, 0x0808);
5528c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_REC_SEL, 0x0000);
5538c2ecf20Sopenharmony_ci		oxygen_write_ac97(chip, 1, AC97_REC_GAIN, 0x0000);
5548c2ecf20Sopenharmony_ci		oxygen_ac97_set_bits(chip, 1, 0x6a, 0x0040);
5558c2ecf20Sopenharmony_ci	}
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cistatic void oxygen_shutdown(struct oxygen *chip)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->reg_lock);
5618c2ecf20Sopenharmony_ci	chip->interrupt_mask = 0;
5628c2ecf20Sopenharmony_ci	chip->pcm_running = 0;
5638c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
5648c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
5658c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->reg_lock);
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_cistatic void oxygen_card_free(struct snd_card *card)
5698c2ecf20Sopenharmony_ci{
5708c2ecf20Sopenharmony_ci	struct oxygen *chip = card->private_data;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	oxygen_shutdown(chip);
5738c2ecf20Sopenharmony_ci	if (chip->irq >= 0)
5748c2ecf20Sopenharmony_ci		free_irq(chip->irq, chip);
5758c2ecf20Sopenharmony_ci	flush_work(&chip->spdif_input_bits_work);
5768c2ecf20Sopenharmony_ci	flush_work(&chip->gpio_work);
5778c2ecf20Sopenharmony_ci	chip->model.cleanup(chip);
5788c2ecf20Sopenharmony_ci	kfree(chip->model_data);
5798c2ecf20Sopenharmony_ci	mutex_destroy(&chip->mutex);
5808c2ecf20Sopenharmony_ci	pci_release_regions(chip->pci);
5818c2ecf20Sopenharmony_ci	pci_disable_device(chip->pci);
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ciint oxygen_pci_probe(struct pci_dev *pci, int index, char *id,
5858c2ecf20Sopenharmony_ci		     struct module *owner,
5868c2ecf20Sopenharmony_ci		     const struct pci_device_id *ids,
5878c2ecf20Sopenharmony_ci		     int (*get_model)(struct oxygen *chip,
5888c2ecf20Sopenharmony_ci				      const struct pci_device_id *id
5898c2ecf20Sopenharmony_ci				     )
5908c2ecf20Sopenharmony_ci		    )
5918c2ecf20Sopenharmony_ci{
5928c2ecf20Sopenharmony_ci	struct snd_card *card;
5938c2ecf20Sopenharmony_ci	struct oxygen *chip;
5948c2ecf20Sopenharmony_ci	const struct pci_device_id *pci_id;
5958c2ecf20Sopenharmony_ci	int err;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	err = snd_card_new(&pci->dev, index, id, owner,
5988c2ecf20Sopenharmony_ci			   sizeof(*chip), &card);
5998c2ecf20Sopenharmony_ci	if (err < 0)
6008c2ecf20Sopenharmony_ci		return err;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	chip = card->private_data;
6038c2ecf20Sopenharmony_ci	chip->card = card;
6048c2ecf20Sopenharmony_ci	chip->pci = pci;
6058c2ecf20Sopenharmony_ci	chip->irq = -1;
6068c2ecf20Sopenharmony_ci	spin_lock_init(&chip->reg_lock);
6078c2ecf20Sopenharmony_ci	mutex_init(&chip->mutex);
6088c2ecf20Sopenharmony_ci	INIT_WORK(&chip->spdif_input_bits_work,
6098c2ecf20Sopenharmony_ci		  oxygen_spdif_input_bits_changed);
6108c2ecf20Sopenharmony_ci	INIT_WORK(&chip->gpio_work, oxygen_gpio_changed);
6118c2ecf20Sopenharmony_ci	init_waitqueue_head(&chip->ac97_waitqueue);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	err = pci_enable_device(pci);
6148c2ecf20Sopenharmony_ci	if (err < 0)
6158c2ecf20Sopenharmony_ci		goto err_card;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	err = pci_request_regions(pci, DRIVER);
6188c2ecf20Sopenharmony_ci	if (err < 0) {
6198c2ecf20Sopenharmony_ci		dev_err(card->dev, "cannot reserve PCI resources\n");
6208c2ecf20Sopenharmony_ci		goto err_pci_enable;
6218c2ecf20Sopenharmony_ci	}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	if (!(pci_resource_flags(pci, 0) & IORESOURCE_IO) ||
6248c2ecf20Sopenharmony_ci	    pci_resource_len(pci, 0) < OXYGEN_IO_SIZE) {
6258c2ecf20Sopenharmony_ci		dev_err(card->dev, "invalid PCI I/O range\n");
6268c2ecf20Sopenharmony_ci		err = -ENXIO;
6278c2ecf20Sopenharmony_ci		goto err_pci_regions;
6288c2ecf20Sopenharmony_ci	}
6298c2ecf20Sopenharmony_ci	chip->addr = pci_resource_start(pci, 0);
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	pci_id = oxygen_search_pci_id(chip, ids);
6328c2ecf20Sopenharmony_ci	if (!pci_id) {
6338c2ecf20Sopenharmony_ci		err = -ENODEV;
6348c2ecf20Sopenharmony_ci		goto err_pci_regions;
6358c2ecf20Sopenharmony_ci	}
6368c2ecf20Sopenharmony_ci	oxygen_restore_eeprom(chip, pci_id);
6378c2ecf20Sopenharmony_ci	err = get_model(chip, pci_id);
6388c2ecf20Sopenharmony_ci	if (err < 0)
6398c2ecf20Sopenharmony_ci		goto err_pci_regions;
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	if (chip->model.model_data_size) {
6428c2ecf20Sopenharmony_ci		chip->model_data = kzalloc(chip->model.model_data_size,
6438c2ecf20Sopenharmony_ci					   GFP_KERNEL);
6448c2ecf20Sopenharmony_ci		if (!chip->model_data) {
6458c2ecf20Sopenharmony_ci			err = -ENOMEM;
6468c2ecf20Sopenharmony_ci			goto err_pci_regions;
6478c2ecf20Sopenharmony_ci		}
6488c2ecf20Sopenharmony_ci	}
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	pci_set_master(pci);
6518c2ecf20Sopenharmony_ci	card->private_free = oxygen_card_free;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	configure_pcie_bridge(pci);
6548c2ecf20Sopenharmony_ci	oxygen_init(chip);
6558c2ecf20Sopenharmony_ci	chip->model.init(chip);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	err = request_irq(pci->irq, oxygen_interrupt, IRQF_SHARED,
6588c2ecf20Sopenharmony_ci			  KBUILD_MODNAME, chip);
6598c2ecf20Sopenharmony_ci	if (err < 0) {
6608c2ecf20Sopenharmony_ci		dev_err(card->dev, "cannot grab interrupt %d\n", pci->irq);
6618c2ecf20Sopenharmony_ci		goto err_card;
6628c2ecf20Sopenharmony_ci	}
6638c2ecf20Sopenharmony_ci	chip->irq = pci->irq;
6648c2ecf20Sopenharmony_ci	card->sync_irq = chip->irq;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	strcpy(card->driver, chip->model.chip);
6678c2ecf20Sopenharmony_ci	strcpy(card->shortname, chip->model.shortname);
6688c2ecf20Sopenharmony_ci	sprintf(card->longname, "%s at %#lx, irq %i",
6698c2ecf20Sopenharmony_ci		chip->model.longname, chip->addr, chip->irq);
6708c2ecf20Sopenharmony_ci	strcpy(card->mixername, chip->model.chip);
6718c2ecf20Sopenharmony_ci	snd_component_add(card, chip->model.chip);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	err = oxygen_pcm_init(chip);
6748c2ecf20Sopenharmony_ci	if (err < 0)
6758c2ecf20Sopenharmony_ci		goto err_card;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	err = oxygen_mixer_init(chip);
6788c2ecf20Sopenharmony_ci	if (err < 0)
6798c2ecf20Sopenharmony_ci		goto err_card;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	if (chip->model.device_config & (MIDI_OUTPUT | MIDI_INPUT)) {
6828c2ecf20Sopenharmony_ci		unsigned int info_flags =
6838c2ecf20Sopenharmony_ci				MPU401_INFO_INTEGRATED | MPU401_INFO_IRQ_HOOK;
6848c2ecf20Sopenharmony_ci		if (chip->model.device_config & MIDI_OUTPUT)
6858c2ecf20Sopenharmony_ci			info_flags |= MPU401_INFO_OUTPUT;
6868c2ecf20Sopenharmony_ci		if (chip->model.device_config & MIDI_INPUT)
6878c2ecf20Sopenharmony_ci			info_flags |= MPU401_INFO_INPUT;
6888c2ecf20Sopenharmony_ci		err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
6898c2ecf20Sopenharmony_ci					  chip->addr + OXYGEN_MPU401,
6908c2ecf20Sopenharmony_ci					  info_flags, -1, &chip->midi);
6918c2ecf20Sopenharmony_ci		if (err < 0)
6928c2ecf20Sopenharmony_ci			goto err_card;
6938c2ecf20Sopenharmony_ci	}
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	oxygen_proc_init(chip);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->reg_lock);
6988c2ecf20Sopenharmony_ci	if (chip->model.device_config & CAPTURE_1_FROM_SPDIF)
6998c2ecf20Sopenharmony_ci		chip->interrupt_mask |= OXYGEN_INT_SPDIF_IN_DETECT;
7008c2ecf20Sopenharmony_ci	if (chip->has_ac97_0 | chip->has_ac97_1)
7018c2ecf20Sopenharmony_ci		chip->interrupt_mask |= OXYGEN_INT_AC97;
7028c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
7038c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->reg_lock);
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	err = snd_card_register(card);
7068c2ecf20Sopenharmony_ci	if (err < 0)
7078c2ecf20Sopenharmony_ci		goto err_card;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	pci_set_drvdata(pci, card);
7108c2ecf20Sopenharmony_ci	return 0;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_cierr_pci_regions:
7138c2ecf20Sopenharmony_ci	pci_release_regions(pci);
7148c2ecf20Sopenharmony_cierr_pci_enable:
7158c2ecf20Sopenharmony_ci	pci_disable_device(pci);
7168c2ecf20Sopenharmony_cierr_card:
7178c2ecf20Sopenharmony_ci	snd_card_free(card);
7188c2ecf20Sopenharmony_ci	return err;
7198c2ecf20Sopenharmony_ci}
7208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(oxygen_pci_probe);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_civoid oxygen_pci_remove(struct pci_dev *pci)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	snd_card_free(pci_get_drvdata(pci));
7258c2ecf20Sopenharmony_ci}
7268c2ecf20Sopenharmony_ciEXPORT_SYMBOL(oxygen_pci_remove);
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
7298c2ecf20Sopenharmony_cistatic int oxygen_pci_suspend(struct device *dev)
7308c2ecf20Sopenharmony_ci{
7318c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
7328c2ecf20Sopenharmony_ci	struct oxygen *chip = card->private_data;
7338c2ecf20Sopenharmony_ci	unsigned int saved_interrupt_mask;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	if (chip->model.suspend)
7388c2ecf20Sopenharmony_ci		chip->model.suspend(chip);
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->reg_lock);
7418c2ecf20Sopenharmony_ci	saved_interrupt_mask = chip->interrupt_mask;
7428c2ecf20Sopenharmony_ci	chip->interrupt_mask = 0;
7438c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
7448c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
7458c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->reg_lock);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	flush_work(&chip->spdif_input_bits_work);
7488c2ecf20Sopenharmony_ci	flush_work(&chip->gpio_work);
7498c2ecf20Sopenharmony_ci	chip->interrupt_mask = saved_interrupt_mask;
7508c2ecf20Sopenharmony_ci	return 0;
7518c2ecf20Sopenharmony_ci}
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_cistatic const u32 registers_to_restore[OXYGEN_IO_SIZE / 32] = {
7548c2ecf20Sopenharmony_ci	0xffffffff, 0x00ff077f, 0x00011d08, 0x007f00ff,
7558c2ecf20Sopenharmony_ci	0x00300000, 0x00000fe4, 0x0ff7001f, 0x00000000
7568c2ecf20Sopenharmony_ci};
7578c2ecf20Sopenharmony_cistatic const u32 ac97_registers_to_restore[2][0x40 / 32] = {
7588c2ecf20Sopenharmony_ci	{ 0x18284fa2, 0x03060000 },
7598c2ecf20Sopenharmony_ci	{ 0x00007fa6, 0x00200000 }
7608c2ecf20Sopenharmony_ci};
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_cistatic inline int is_bit_set(const u32 *bitmap, unsigned int bit)
7638c2ecf20Sopenharmony_ci{
7648c2ecf20Sopenharmony_ci	return bitmap[bit / 32] & (1 << (bit & 31));
7658c2ecf20Sopenharmony_ci}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_cistatic void oxygen_restore_ac97(struct oxygen *chip, unsigned int codec)
7688c2ecf20Sopenharmony_ci{
7698c2ecf20Sopenharmony_ci	unsigned int i;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	oxygen_write_ac97(chip, codec, AC97_RESET, 0);
7728c2ecf20Sopenharmony_ci	msleep(1);
7738c2ecf20Sopenharmony_ci	for (i = 1; i < 0x40; ++i)
7748c2ecf20Sopenharmony_ci		if (is_bit_set(ac97_registers_to_restore[codec], i))
7758c2ecf20Sopenharmony_ci			oxygen_write_ac97(chip, codec, i * 2,
7768c2ecf20Sopenharmony_ci					  chip->saved_ac97_registers[codec][i]);
7778c2ecf20Sopenharmony_ci}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_cistatic int oxygen_pci_resume(struct device *dev)
7808c2ecf20Sopenharmony_ci{
7818c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
7828c2ecf20Sopenharmony_ci	struct oxygen *chip = card->private_data;
7838c2ecf20Sopenharmony_ci	unsigned int i;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_DMA_STATUS, 0);
7868c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, 0);
7878c2ecf20Sopenharmony_ci	for (i = 0; i < OXYGEN_IO_SIZE; ++i)
7888c2ecf20Sopenharmony_ci		if (is_bit_set(registers_to_restore, i))
7898c2ecf20Sopenharmony_ci			oxygen_write8(chip, i, chip->saved_registers._8[i]);
7908c2ecf20Sopenharmony_ci	if (chip->has_ac97_0)
7918c2ecf20Sopenharmony_ci		oxygen_restore_ac97(chip, 0);
7928c2ecf20Sopenharmony_ci	if (chip->has_ac97_1)
7938c2ecf20Sopenharmony_ci		oxygen_restore_ac97(chip, 1);
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	if (chip->model.resume)
7968c2ecf20Sopenharmony_ci		chip->model.resume(chip);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	oxygen_write16(chip, OXYGEN_INTERRUPT_MASK, chip->interrupt_mask);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
8018c2ecf20Sopenharmony_ci	return 0;
8028c2ecf20Sopenharmony_ci}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ciSIMPLE_DEV_PM_OPS(oxygen_pci_pm, oxygen_pci_suspend, oxygen_pci_resume);
8058c2ecf20Sopenharmony_ciEXPORT_SYMBOL(oxygen_pci_pm);
8068c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_civoid oxygen_pci_shutdown(struct pci_dev *pci)
8098c2ecf20Sopenharmony_ci{
8108c2ecf20Sopenharmony_ci	struct snd_card *card = pci_get_drvdata(pci);
8118c2ecf20Sopenharmony_ci	struct oxygen *chip = card->private_data;
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	oxygen_shutdown(chip);
8148c2ecf20Sopenharmony_ci	chip->model.cleanup(chip);
8158c2ecf20Sopenharmony_ci}
8168c2ecf20Sopenharmony_ciEXPORT_SYMBOL(oxygen_pci_shutdown);
817